Signalr Connect Disconnect and reconnect

asp.net-mvc, c#, signalr

Solution

- SignalR will create the group when you call `Groups.Add()` for the first time for a particular `projID`. It won't throw an error.

- `OnDisconnected` is called whenever the connection goes away. If you call `Stop()` there is a clean disconnect and the `OnDisconnected` method is called immediately. If you just shut the browser, the `OnDisconnected` method will usually be called after a delay of about 30 seconds (there's a configuration switch to control this)

- Users are tied to specific groups based on their connection id's. If a user comes back with a different connection id you will have to add it again to the appropriate group. You can have a look at the Chat sample provided with SignalR to see how cases such as this can be handled.

Problem

I am wanting to make sure that I am implementing the group feature correctly for the SignalR library. What I am doing is allowing users to ask for help for a specific project. The user that started the project can add other users to a collaboration table for their project. ``` Collaboration ( UserID Uniqueidentifier, ProjectID INT ) ``` If either user goes into collaboration mode I want to add that user to a group so if another user logs on and goes into collaboration mode they are added to the same group. The groups are always named the `ProjectID`. So when a user logs on and opens a projects if that project is in the collaboration table I add them to the `Groups.Add(Conext.ConnectionId,projID)`; Here are my questions: When a user connects from the client and the OnConnected is called if no group with the projID exists will this throw an error or will signalr just create that group on the fly? ``` public override Task OnConnected(string projID) { return Groups.Add(this.Context.ConnectionId, projID); } ``` When a client closes their browser, is that when the OnDisconnected is called? And if that user for some reason is not in the said projID group will this throw and error or will signalr handle this? ``` public override Task OnConnected(string projID) { return Groups.Add(this.Context.ConnectionId, projID); } ``` For the OnReconnected, does this mean that if a user logs off and does something else then logs back on that they are automatically added back to the group they where part of before the connect was lost? ``` public override Task OnReconnected(string projID) { return Clients.Group(projID).rejoined(Context.ConnectionId, DateTime.Now.ToString()); } ``` For all of the methods above do I need to call the base method of each overrided method?

Original source