Reputation: 16219
I have an MVC application.
I have implemented singalR to receive live notifications but how to get only user specific notifications.
NotificationSend.cs
public class NotificationSend : Hub
{
private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationSend>();
public static ConcurrentDictionary<string, MyUserType> MyUsers = new ConcurrentDictionary<string, MyUserType>();
public override Task OnConnected()
{
MyUsers.TryAdd(Context.ConnectionId, new MyUserType() { ConnectionId = Context.ConnectionId });
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
MyUserType garbage;
MyUsers.TryRemove(Context.ConnectionId, out garbage);
return base.OnDisconnected(stopCalled);
}
public static void SendToUser(string messageText)
{
hubContext.Clients.Client(MyUsers.Keys.ToList().FirstOrDefault()).Notification(messageText);
}
public static void StopLoader(string messageText)
{
hubContext.Clients.Client(MyUsers.Keys.ToList().FirstOrDefault()).Stoploader(messageText);
}
}
public class MyUserType
{
public string ConnectionId { get; set; }
}
HomeController.cs
public class HomeController : Controller
{
public async Task<ActionResult> SaveData()
{
foreach (var mydata in DataList)
{
// save data code and show below message on UI
NotificationSend.SendToUser(mydata.Name + ": Data saved");
I'm able to get notification on UI perfectly fine but problem is
If user A using his own machine and his login he should get only his notification , I know webapp url is same.
for that i make below change but not any notification is visible after this change.
string UserID = User.Identity.Name;
hubContext.Clients.User(UserID).Notification(mydata.Name + ": Data saved");
Layout.js
$(function () {
var notification = $.connection.notificationSend;
console.log(notification);
notification.client.Notification = function (Count) {
$('#liveupdate').empty();
$('#liveupdate').show();
$('#liveupdate').append(Count);
};
$.connection.hub.start().done(function () {
var connectionId = $.connection.hub.id;
console.log("Connected Successfully");
}).fail(function (response) {
console.log("not connected" + response);
});
});
Upvotes: 0
Views: 1970
Reputation: 1675
Add a static class its instance will be created once and will persist information in memory like the instance of context
public static class NotificationsResourceHandler
{
private static readonly IHubContext myContext;
public static Dictionary<string, string> Groups;
static NotificationsResourceHandler()
{
myContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
Groups = new Dictionary<string, string>();
}
public static void BroadcastNotification(dynamic model, NotificationType notificationType, string userName)
{
myContext.Clients.Group(userName).PushNotification(new { Data = model, Type = notificationType.ToString() });
}
}
and in your Hub
[HubName("yourHub")]
public class MyHub : Hub
{
public override Task OnConnected()
{
var userEmail = Context.QueryString["useremail"]?.ToLower();
if (userEmail == null) throw new Exception("Unable to Connect to Signalr hub");
if (NotificationsResourceHandler.Groups.All(x => x.Value != userEmail))
{
NotificationsResourceHandler.Groups.Add(Context.ConnectionId, userEmail);
Groups.Add(Context.ConnectionId, userEmail);
}
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
NotificationsResourceHandler.Groups.Remove(Context.ConnectionId);
Clients.All.removeConnection(Context.ConnectionId);
return base.OnDisconnected(stopCalled);
}
}
Notification will be pushed to individual groups, for your problem you should create a separate group for each user as provided in code.
Upvotes: 2
Reputation: 91
Here is my example code in VB.Net (you may convert it to C#):
Public Class SignalRHub
Inherits Hub
Private Shared hubContext As IHubContext = GlobalHost.ConnectionManager.GetHubContext(Of SignalRHub)()
Public Sub SendToAll(ByVal msg As String)
hubContext.Clients.All.addNewMessageToPage(msg)
End Sub
Public Shared Sub SendToUser(ByVal user As String, ByVal msg As String)
hubContext.Clients.Group(user).addNewMessageToPage(msg)
End Sub
Public Overrides Function OnConnected() As Task
Dim name As String = Context.User.Identity.Name
Groups.Add(Context.ConnectionId, name)
Return MyBase.OnConnected()
End Function
End Class
You have to use Group. Basically what I do is 1 group is for 1 user. Define by the username.
Then just call the function:
Dim user As User = idb.Users.Where(Function(a) a.id = userid).FirstOrDefault
Dim msg as string = "Any notification message"
SignalRHub.SendToUser(user.UserName, msg)
Lastly, javascript code to trigger that:
var notification = $.connection.signalRHub;
notification.client.addNewMessageToPage = function (msg) {
$("#notification").prepend(msg);
}
ID notification where you want to put the notification message.
Upvotes: 1