SignalR Join Group new Context.ConnectionId on browser refresh -


my app has bunch of usergroups.

there admin page can send out notifications 1 or many of these usergroups.

i have hub admin can post notifications after saved database via ajax call webapi method. when returns in ajax success section call method on server hub posts notification realtime 1 or more groups. groups indicated array of int creates group names converting int .tostring().

the clients have widget on home page shows notifications. of users can part of more 1 usergroup, have dropdownlist @ top of page specifies usergroupd viewing. may part of many usergroups view 1 @ time.

thus doing jabbr chatrooms guess, don't want set quite because app has different intention. when client logs in join group based on value of selected item in dropdownlist. did notice whenever refresh page client 'join' server method called again new connectionid. right? don't want have more connections there are. still need disconnect client group , rejoin them new group when change dropdownlist.

i looking demos find either easy demo or sophisticated 1 , aiming in between, since there other areas of site intend add signalr functionality don't want lock down object model much.

so on hub got:

public void join(string groupname) {   groups.add(context.connectionid, groupname); }  public void sendnotificationstogroups(string title, string description, int[] groups) {         foreach (int in groups) {             clients.othersingroup(i.tostring()).addmessage(title, description);         } } 

the admin page calls sendnotificationstogroups , works fine. see passes array of ints groups broadcast notification to. .tostring int create groupnames.

the client following:

var hub = $.connection.myhub;  hub.client.addmessage = function (title, description) {   $('#notification').find('tr:first').before('<tr><td>'+title+description'</td></tr>'); }  $.connection.hub.start().done(function () {     var groupid = $('#usergroupid').val();     newshub.server.join(groupid); } 

my main concern again join method addmessage part works!! admin sends notifications , clients see them. issue if client refreshes browser join called again on server (i got breakpoint set on it) , has new context.connectionid. bad? causing more connections necessary. there javascript/jquery call should make ensure connection if destroyed if window closed of client leaves page, browser refresh issue.

thanks ideas.

signalr hooks window's unload event , calls .stop() on connection, asynchronous message might not happen immediately. also, server side automatically detect , weed out "dead" connections part of housekeeping chores.

that said, best keep signalr connection alive if can rather disconnecting/reconnecting on every page navigation. disconnecting , reconnecting kinda defeats purpose , miss messages between page navigations.


Comments