If you want to notify other clients/users throughout the application ,you not need to worry about the connection because signalr new connection is created every time you visit other pages in the web app.
we can leverage the users feature of signalr to achieve the same. see the example below:
Here we are creating based on the projectid and all others users of that group get notified when we access the $.connection.notificationHub.server.NotifyOthersInGroup(projectid,projectname); from client javascript file
//Hub Example of SignalR
public class NotificationHub : Hub {
public static readonly ConcurrentDictionary<string, HashSet<string>>
ProjectLockUsers = new ConcurrentDictionary<string,HashSet<string>>();
public void JoinGroup(int projectid)
{
string groupname = string.Format("ProjectLock_{0}", projectid);
Groups.Add(Context.ConnectionId, groupname);
AddUserToProjectLockGroup(groupname, Context.User.Identity.Name);
}
public void NotifyOthersInGroup(int projectid,string name)
{
string groupname = string.Format("ProjectLock_{0}", projectid);
var allusers=null as HashSet<string>;
if (ProjectLockUsers.TryGetValue(groupname, out allusers))
{
allusers.Remove(Context.User.Identity.Name);
Clients.Users(allusers.ToList()).notifyUnlock(name);
}
}
public override Task OnConnected()
{
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
return base.OnDisconnected(stopCalled);
}
private void AddUserToProjectLockGroup(string projectId,string userId)
{
var userIds = null as HashSet<string>;
if (Sessions.TryGetValue(projectId, out userIds) == false)
{
userIds = Sessions[projectId] = new HashSet<string>();
}
userIds.Add(userId);
ProjectLockUsers[projectId] = userIds;
}
}