locked
SignalR - How to access user mapping in other server side class to send notifications to a particular user RRS feed

  • Question

  • User1804044093 posted

    I have implemented In-memory storage mapping in my MVC project to store the userId and its related connectionsId as shown in the mapping link below. The problem is that I am not able to access the mapping instance in the other server side class and send notification to a particular user who are connected. How should I modify Hub or SendNotification class to send Notification.

    // here is the sample hub class with onconnected and disconnected same as in mapping link
    public class NotificationHub : Hub
    {
        private readonly static ConnectionMapping<string> _connections =  new ConnectionMapping<string>();
    
        public void SendChatMessage(string who, string message)
        {
            string name = Context.User.Identity.Name;
    
            foreach (var connectionId in _connections.GetConnections(who))
            {
                Clients.Client(connectionId).addChatMessage(name + ": " + message);
            }
        }
    }
    
    // other server class where to call hub
    public class SendNotification
    {
        Public void SaveToDb(string userName, class entity)
        {
            // save to database call
            // send Notification to User userName through SignalR if user Exists in Mapping
        }
    }

    Here's a SignalR Mapping link which I have implemented (https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections)!

    Here's How to access hub outside (https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#callfromoutsidehub)!

    Saturday, June 10, 2017 1:00 PM

All replies

  • User1068175894 posted

    var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
    foreach (var connectionId in _connections.GetConnections(userName))
    {
    	context.Clients.Client(connectionId).addChatMessage(name + ": " + message);
    }
    

    Saturday, June 10, 2017 11:59 PM
  • User1967761114 posted

    Hi Hyder Ahmed,

    According to your description, you could use GlobalHost.ConnectionManager.GetHubContext<NotificationHub>(), see the following code:

    (1) NotificationHub.cs

    [HubName("NotificationHub")]
    public class NotificationHub : Hub
    {
         private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();
         public static void SendNotification(string who, string message)
         {
             IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
             foreach (var connectionId in _connections.GetConnections(who))
             {
                 //get client from hubcontext by connectionId
                 var client = context.Clients.Client(connectionId);
                 if (client != null)
                 {
                      //notify client
                      client.addChatMessage(who + ":" + message);
                 }
              }
         }
    }

    (2) SendNotification example:

    public class SendNotification
    {
         public void SaveToDb(string userName)
         {
              // save to database call
              // send Notification to User userName through SignalR if user Exists in Mapping
              NotificationHub.SendNotification("username", "Save To Db succeeds");
         }
    }

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Monday, June 12, 2017 12:26 PM