The ASP.NET Core SignalR repository on
GitHub.
SignalR is an open source library and allows developers to simplify adding
real-time web functionality to applications.
"real-time web" functionality means that the server-side code can instantly push content to connected clients.
"real-time web" functionality means that the server-side code can instantly push content to connected clients.
Examples
- Chat applications
- Notification applications (Team meet, share ideas)
- Live dashboards (display instantly updated sales information).
The SignalR API allows server-side code to call
JavaScript functions on connected clients via remote procedure calls (RPCs).
Feature of SignalR
- It automatically takes care of connection management.
- It can send messages to all connected clients at the same time or to a specific client or group of clients. (Chat room).
- It can automatically scales to handle increasing traffic.
To handle real-time communication, SignalR supports (These are called
transports):
- WebSockets
- Server-Sent Events
- Long Polling
SignalR lies on top of the lower-level transports.
SignalR automatically chooses the best transport method that is within the
capabilities of the server and client.
- If WebSockets (the most efficient of the transports) are not supported by the server or browser, SignalR will fall back on Server-Sent Events.
- If Server-Sent Events are not supported, SignalR will fall back on Long Polling.
- When SignalR establishes a connection, it will start sending keep-alive messages to verify that the connection is still alive. If the connection is not alive, SignalR will throw an exception.
- The WebSocket protocol enables two-way, real-time communication between clients and servers in web-based applications.
Hubs
Hubs are used by SignalR to communicate between servers and clients. The hub
allows a client and server to call methods on each other.
Configure the SignalR middleware by registering the service in the
Startup.ConfigureServices() {.. services.AddSignalR(); }
Set up the SignalR routes by calling endpoints.MapHub in the
Startup.Configure()
app.UseEndpoints(endpoints =>
{
...
endpoints.MapHub<ChatHub>("/WhatsupChatHub");
});
The Hub class (HubCallerContext) property provide information about the
connection:
ConnectionAborted, ConnectionId,
Features, Items, User, UserIdentifier
The Hub class methods: an Abort method and GetHttpContext extension
method.
The hubs are impermanent (last for a short time) and transient, should not
store state in a property on the hub class, because every hub method call is
executed on a new instance of the hub.
IHubCallerClients
The hub will handle client-server communication. Create a
whatsupchat\Hubs\WhatsupChatHub.cs class
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
namespace whatsupchat.Hubs;
{
public class WhatsupChatHub : Hub
{
public async Task MessageSender(string user, string message)
{
await Clients.All.SendAsync("MessageReceiver", user, message);
}
}
}
using Microsoft.AspNetCore.SignalR;
namespace whatsupchat.Hubs;
{
public class WhatsupChatHub : Hub
{
public async Task MessageSender(string user, string message)
{
await Clients.All.SendAsync("MessageReceiver", user, message);
}
}
}
"Clients.All" property that invokes a method on all the clients connected to
this hub.
The Clients object implements the IHubCallerClients interface (public
IHubCallerClients Clients { get; set; }). This interface has the following
properties:
- All — All clients connected to the hub.
- Caller — Gets a caller to the connection that triggered the current invocation.
- Others — Gets a caller to all connections except the one that triggered the current invocation.
It also includes methods such as:
- AllExcept — All clients connected to the hub, but excludes the specified connections passed as a read-only list of strings.
- Client — A specific client connection.
- Clients — The specified read-only list of client connections.
- Group — All connections in the specified group.
- GroupExcept — All connections in the specified group, but excludes the specified connections passed as a read-only list of strings.
- Groups — All connections in the specified read-only list of groups.
- OthersInGroup — Gets a caller to all connections in the specified group, except the one which triggered the current invocation.
- User — All connections associated with the specified user.
- Users — All connections associated with the users specified in the read-only list of strings.
The WhatsupChatHub class inherits from the SignalR Hub class. This Hub
base class is responsible for managing connections, groups, and
messaging. The following properties:
- Clients — Gets/sets an object on the clients that are connected to this hub.
- Context — Get/set the hub caller context.
- Groups — Get/set the group manager.
The Hub class also has the following properties:
- OnConnectedAsync — Called when a new connection is established with the hub and returns a task representing the asynchronous connect.
- OnDisconnectedAsync — Called when the connection to the hub is terminated and returns a task representing the asynchronous disconnect.
- Dispose — Dispose of all the resources currently in use by this hub instance.
No comments:
Post a Comment