I've started working with SignalR and was trying to figure out when a Hub Reconnect occurs. I didn't find any satisfying explanation on the web. Can someone explain when/why a reconnect occurs?
-
1What is the behavour when is host is suddenly down ? All clients connected to the host don't receive any messages. How do you solve or catch that ? – Kris Nobels Mar 05 '14 at 15:08
-
Hi @LeMoustique I believe that's worth a separate question in SO. – gsharp Mar 05 '14 at 15:14
1 Answers
A hub reconnect occurs when a client goes offline then regains connectivity shortly after. SignalR configuration values largely determine the time stamps of the following examples so do not take the times verbatim.
Here are several examples and their outcomes (time format m:ss) involving reconnecting behavior:
When I Mention the following i am referring to the server side Hub method
- OnConnected
- OnDisconnected
- OnReconnected
1)
0:00 - Client connects to server, OnConnected is triggered
0:10 - Client loses connection due to ISP issues (and realizes it loses connection)
0:15 - Client Regains connectivity
0:16 - OnReconnected event is triggered
2)
0:00 - Client connects to server, OnConnected is triggered
0:10 - Client loses connection due to pulling ethernet cable (doesn't realize it's disconnected)
0:15 - Client Regains connectivity
Two things can happen here
A: 0:16 - Nothing happens and client continues on with its previous connection
B: 0:~45 - Client Realizes its disconnected *
B: 0:46 - Client transitions into the reconnecting state
B: 0:47 - Client successfully reconnects and the OnReconnected event is triggered.
3)
0:00 - Client connects to server, OnConnected is triggered
0:10 - Client loses connection due to pulling ethernet cable (doesn't realize it's disconnected)
0:~45 - Client Realizes its disconnected *
0:46 - Client transitions into the reconnecting state
1:15 - Server determines that client has been gone for too long and then forgets about it, queuing up a "disconnect" command for the client to receive if it reconnects slightly later. ***
1:15 - OnDisconnected is triggered
1:16 - Client regains connectivity
1:17 - Client does a "soft" reconnect (does not trigger OnReconnected)
1:18 - Client retrieves the "disconnect" command
1:19 - Client calls "stop" and does a soft disconnect (does not trigger OnDisconnected)
4)
0:00 - Client connects to server, OnConnected is triggered
0:10 - Client loses connection due to pulling ethernet cable (doesn't realize it's disconnected)
0:~45 - Client Realizes its disconnected *
0:46 - Client transitions into the reconnecting state
1:15 - Server determines that client has been gone for too long and then forgets about it, queuing up a "disconnect" command for the client to receive if it reconnects slightly later. ***
1:15 - OnDisconnected is triggered
1:30 - Client stops trying to reconnect (been trying too long) **
1:30 - Client transitions into disconnected state
* Due to client side keep alive check: Used to determine when a client is offline due to lack of keep alives. Not utilized for the long polling transport
** Due to client side disconnect timeout: Used to determine when a client has been reconnecting for too long of a period and chances are the server has forgotten about the client during the time
*** Due to server disconnect timeout: Used to determine when a client should be forgotten about. It's a time span that starts accruing once a connection is tagged as dead on the server. Ultimately the server queues a disconnect command for the client's topic which tells the client (if it reconnects) that it needs to start a fresh connection. The command will disappear from the server when the topic is cleaned up.

- 20,030
- 7
- 43
- 238

- 18,061
- 6
- 49
- 72
-
7Excellent Answer! The SignalR guys should put this in their wiki :-) Thanks a lot. – gsharp Jan 23 '13 at 08:45
-
4when the client reconnects, what happens to the Context.ConnectionID, does it remain the same or does it change? Also when singalR client is "reconnected" to server, in that process, is OnDisconnected ever called? in other words does "reconnected" means (OnDisconnected+OnConnected)? – Bhavin Jun 04 '13 at 13:29
-
3Connection ID will remain the same. Reconnected will never be called AFTER OnDisconnected. Therefore you can go from OnConnected -> OnReconnected and OnConnected -> OnDisconnected but you can never go from OnDisconnected -> OnReconnected. – N. Taylor Mullen Jun 04 '13 at 17:32
-
@N.TaylorMullen thanks for your answer. Got one doubt. In case 3, at step 6 OnDisconnected is triggered, then regains connectivity and last step performs a soft disconnect. Does this means he has completly lost connection and can't send/receive messages? – kzfabi Jul 10 '13 at 15:24
-
1That is correct, you can always handle the disconnected event on the client and then re-start your connection to regain connectivity – N. Taylor Mullen Jul 10 '13 at 17:32
-
2An excellent and official explanation is also [here](http://www.asp.net/signalr/overview/signalr-20/hubs-api/handling-connection-lifetime-events). – John Feb 03 '14 at 12:42
-
@N.TaylorMullen If OnDisconnected -> OnReconnected will never be happen and ConnectionId remain same so why official example check ConnectionId in user connections and add it if not exist. Here link http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections in On Memory Storage section – Freshblood Jan 12 '15 at 18:24
-
@AmirHossein Mehrvarzi can it possible the one user can connect multiple time on same ConnectionID to server. – Khalid Mar 05 '16 at 07:37
-
@Khalid yes, take a look at http://forums.asp.net/t/1887454.aspx?How+to+manually+set+the+connection+id – Amirhossein Mehrvarzi Mar 06 '16 at 11:33