I have been trying to add a real-time notification feature to an MVC website using SignalR. I am using Asp.NET Identity for authentication.
As I understood from SignalR documentation the OnConnected method on Hub class should fire whenever a user logs in.
So when I came to debug, if I sign the first user, the OnConnected method called properly. However, when I open another browser(eg: Firefox, Microsoft edge ..) and sign in another user, OnConnected is never called.
My Startup class looks like this:
[assembly: OwinStartup(typeof(NotificationApp.Startup))]
namespace NotificationApp
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.MapSignalR();
}
}
}
Hub class
namespace NotificationApp
{
[Authorize]
public class NotificationHub : Hub {
//This gets called for the first logged in user only
public override Task OnConnected()
{
Clients.Client(Context.ConnectionId).connected(Context.ConnectionId);
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
return base.OnDisconnected(stopCalled);
}
}
}
Client code
$(document).ready(function () {
var notificationHub = $.connection.notificationHub;
//Called for every client properly
$.connection.hub.start().done(function () {
console.log('Notification hub started');
});
//This gets called for the first logged in user only
notificationHub.client.connected = function (message) {
console.log('Hello' + message);
};
});
When you open another tab, you are the same client and are then given a connectionID.
Try changing "Client":
public override Task OnConnected()
{
Clients.Client(Context.ConnectionId).connected(Context.ConnectionId);
return base.OnConnected();
}
To "Caller":
public override Task OnConnected()
{
Clients.Caller.connected(Context.ConnectionId);
return base.OnConnected();
}
Also, move your client function before your hub.start. You should always register one function before your start. See the note regarding in the docs.