简体   繁体   中英

How send notification to groups using SignalR in Asp.net Core

Here we are using asp.net core singnalR alpha2 version. How to send notification for groups (group of users)? If any sample is there for scenario, post here the sample link.

In Hub write method with group.

public class SignalRCommonHub : Hub
{
   
    public void SendToGroup(int groupId, int userId)
    {
        Clients.Group(groupId.ToString()).InvokeAsync("refresh", groupId, userId);
    }
}

In controller called the hub method.

private readonly IHubContext<SignalRCommonHub> isignalRhub;

public SignalRModel(IHubContext<SignalRCommonHub> signalRhub)
{
    this.isignalRhub = signalRhub;
}

public void RefreshPage(int groupId, int userId)
{
    this.isignalRhub.Clients.Group(groupId.ToString()).InvokeAsync("refresh", groupId, userId);
}

In client side not trigger when call method in client side.

User online status update based on groups (group of users) using signalR sample available means post here link. Suggest idea for how to implement the user online status.

Thanks,

from your front end interface, you will need to setup the hub/connection, then make a call to Add/Remove groups using the connectionId and the group id/name.

eg: await _viewerHubContext.Groups.AddAsync(connectionId, groupName);

Then when you broadcast calls, you just use the group id/name and signalR will send to those groups.

eg: return Clients.Group(groupName).InvokeAsync("Send", "SendData", data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM