

To make calls to specific clients, use the properties of the Clients object.

The SendAsync method receives the name of the client method to call and any parameters. Hub.Clients also contains the following methods: MethodĬalls a method on all connected clients except for the specified connectionsĬalls a method on a specific connected clientĬalls a method on specific connected clientsĬalls a method on all connections in the specified groupĬalls a method on all connections in the specified group, except the specified connectionsĬalls a method on multiple groups of connectionsĬalls a method on a group of connections, excluding the client that invoked the hub methodĬalls a method on all connections associated with a specific userĬalls a method on all connections associated with the specified usersĮach property or method in the preceding tables returns an object with a SendAsync method. The Hub class includes a Clients property that contains the following properties for communication between server and client: PropertyĬalls a method on the client that invoked the hub methodĬalls a method on all connected clients except the client that invoked the method For HTTP connections, use this method to get information such as HTTP headers and query strings. Returns the HttpContext for the connection, or null if the connection isn't associated with an HTTP request. Hub.Context also contains the following methods: Method Gets a CancellationToken that notifies when the connection is aborted. For now, this collection isn't needed in most scenarios, so it isn't documented in detail yet. Gets the collection of features available on the connection. Data can be stored in this collection and it will persist for the connection across different hub method invocations. Gets a key/value collection that can be used to share data within the scope of this connection. Gets the ClaimsPrincipal associated with the current user. By default, SignalR uses the ClaimTypes.NameIdentifier from the ClaimsPrincipal associated with the connection as the user identifier. There's one connection ID for each connection. Gets the unique ID for the connection, assigned by SignalR. The Hub class includes a Context property that contains the following properties with information about the connection: Property For example, a method such as (.) can fail if it's called without await and the hub method completes before SendAsync finishes.

=> await ("ReceiveMessage", user, message) Public async Task SendMessage(string user, string message) Add public methods to the class to make them callable from clients: public class ChatHub : Hub To configure SignalR endpoints, call MapHub, also in Program.cs: app.MapRazorPages() Ĭreate a hub by declaring a class that inherits from Hub. To register the services required by SignalR hubs, call AddSignalR in Program.cs: var builder = WebApplication.CreateBuilder(args) SignalR takes care of everything required to make real-time client-to-server and server-to-client communication possible. The server defines methods that are called from the client and the client defines methods that are called from the server. The SignalR Hubs API enables connected clients to call methods on the server.
