locked
How to send notifications to set of users? RRS feed

  • Question

  • User1807725305 posted

    I am using SignalR for displaying onscreen notifications in my web application(built using Asp.net MVC).

    My question is How to show notifications to specific set of users eg. Display onscreen notifications to all the users with reader role?. The roles and user associated with roles are defined the database.

    I have read it in the documentation where it is mentioned about groups. But i am not sure how to use it.

    Sunday, February 21, 2016 12:34 PM

Answers

  • User-1417985901 posted

    Look at Groups,

    In the OnConnected() or UserAuthorized() override add the user to all the groups of his user-role.

    you can set up a task like this.

    public override Task OnConnected() {
        var Roles = GetUserRolesHere(userId);
        Roles.ForEach(r => {
            JoinGroup("role-" + r);
        });
    }
    
    
    public Task JoinGroup(string Name) {
      return Groups.Add(Context.ConnectionId, Name);
    }
    

    then you can send messages like this

    Clients.Group("role-admin").Notification("hello");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 21, 2016 7:27 PM
  • User61956409 posted

    Hi sharath,

    How to show notifications to specific set of users eg

    You could add users to groups then broadcast messages to specified subsets of connected clients. For more information about working with Groups in SignalR, please check this article.

    http://www.asp.net/signalr/overview/guide-to-the-api/working-with-groups

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2016 6:45 AM

All replies

  • User-1417985901 posted

    Look at Groups,

    In the OnConnected() or UserAuthorized() override add the user to all the groups of his user-role.

    you can set up a task like this.

    public override Task OnConnected() {
        var Roles = GetUserRolesHere(userId);
        Roles.ForEach(r => {
            JoinGroup("role-" + r);
        });
    }
    
    
    public Task JoinGroup(string Name) {
      return Groups.Add(Context.ConnectionId, Name);
    }
    

    then you can send messages like this

    Clients.Group("role-admin").Notification("hello");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 21, 2016 7:27 PM
  • User61956409 posted

    Hi sharath,

    How to show notifications to specific set of users eg

    You could add users to groups then broadcast messages to specified subsets of connected clients. For more information about working with Groups in SignalR, please check this article.

    http://www.asp.net/signalr/overview/guide-to-the-api/working-with-groups

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2016 6:45 AM