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 classwith onconnected and disconnected same asin mapping link
publicclassNotificationHub:Hub{privatereadonlystaticConnectionMapping<string> _connections =newConnectionMapping<string>();publicvoidSendChatMessage(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 classwhere to call hub
publicclassSendNotification{PublicvoidSaveToDb(string userName,class entity){// save to database call// send Notification to User userName through SignalR if user Exists in Mapping}}
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.
None
0 Points
1 Post
SignalR - How to access user mapping in other server side class to send notifications to a partic...
Jun 10, 2017 01:00 PM|Hyder Ahmed|LINK
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'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)!
Participant
1380 Points
608 Posts
Re: SignalR - How to access user mapping in other server side class to send notifications to a pa...
Jun 10, 2017 11:59 PM|JBetancourt|LINK
Please remember to click "Mark as Answer" the responsES that resolved your issue.
Member
520 Points
286 Posts
Re: SignalR - How to access user mapping in other server side class to send notifications to a pa...
Jun 12, 2017 12:26 PM|EvenMa|LINK
Hi Hyder Ahmed,
According to your description, you could use GlobalHost.ConnectionManager.GetHubContext<NotificationHub>(), see the following code:
(1) NotificationHub.cs
(2) SendNotification example:
If you have any other questions, please feel free to contact me any time.
Best Regards
Even