Reputation: 917
Im am writing a one-to-one chat in Signalr in a legacy Webforms project (note VB.net). Basically Im looking for a result like Facebook chat, so that basically as soon as a user logs in, they are classed as 'online'.
I have some success. In my login method, I am adding my logged in user to a static list of OnlineUsers. I have a Chat.aspx page which when I refresh, starts the SignalR hub connection and I can see the logged in User (loaded from OnConnected method).
Obviously I dont want to have to reload the Chat.aspx page to see any users that have just logged in, so my question is, is it safe/efficient to start the connection from say my Masterpage so that all the UI is updated when a user comes on/offline? Im guessing this will cause loads of unneccessary connections?
Here is my code.
Login success (adds to a static list of OnlineMembers
OnlineMember.AddOnlineMember(member.UserId, member.UserName)
Chat.aspx
$.connection.hub.start(function () {
chat.server.getAllOnlineStatus();
});
Upvotes: 1
Views: 745
Reputation: 1112
Raj, I think you would be best off to call a routine the moment someone is actually logging in.
If you have a login routine that adds the users to OnlineUsers list then you can trigger an update of ALL clients by calling a method that updates the list of online users.
A function inside your Chat.aspx page that runs at any time other than login will generate load needlessly. The beauty of SignalR is the persistence to the client(s) and how you can target "All" or "Caller" when triggering client-side routines.
(Here is a server-side hub call to a routine registered in the ASPX. You could pass data to it as an argument if required.)
The following could be done after successful login of a new client: i.e. Clients.Caller.updateOnlineUserList(); -or- Clients.Caller.updateOnlineUserList(someData);
(Obviously you'd need to write the code for "updateOnlineUserList" which would update the user list displayed on the page.)
Upvotes: 0