Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - how to reconnect client to server (how to restart SignalR)

I have the following problem with SignalR with users accessing my website from a mobile device. The problem is that when the phone locks or the user goes to another app, the Disconnect() method is fired on the server (I try sending a message to the server and waiting for it to reply trough SignalR but there is no reply from the server after the Disconnect method is fired on server). After this it seams as the client is no more connected to the server.

How can the client tell the server that it is back?

like image 774
Ryan Avatar asked Sep 24 '12 06:09

Ryan


People also ask

How do I end a SignalR connection?

A SignalR connection can end in any of the following ways: If the client calls the Stop method, a stop message is sent to the server, and both client and server end the SignalR connection immediately. After connectivity between client and server is lost, the client tries to reconnect and the server waits for the client to reconnect.

Why does the SignalR client never automatically reconnect to the server?

Scenario C - reconnecting to the SignalR server after the app was idle/closed for 120 seconds or longer. In this scenario, the SignalR transport protocol has already timed out so the client never automatically reconnects. This explains why the client was sometimes but not always reconnecting on its own.

What happens when client calls stop method in SignalR?

If the client calls the Stop method, a stop message is sent to the server, and both client and server end the SignalR connection immediately. After connectivity between client and server is lost, the client tries to reconnect and the server waits for the client to reconnect.

How to configure SignalR to automatically reconnect to hubconnectionbuilder?

For this to work, the SignalR Javascript Client version should be 3.0.0 or above. We can configure the Javascript client for SignalR to automatically reconnect using the withAutomaticReconnect () method on HubConnectionBuilder. However, it is not the default behavior and we need to call this method explicitly.


2 Answers

You can stop the connection:

$.connection.hub.stop() 

and then restart it via

$.connection.hub.start();

In the next version of SignalR we will take care of this for you, but for the interim you will have to manage it yourself. I'd recommend detecting on the client if the server is dead and then running the stop -> start.

like image 165
N. Taylor Mullen Avatar answered Nov 17 '22 00:11

N. Taylor Mullen


We had a similar situation connecting from mobile devices. In our case, calling .Stop() or trying to dispose the existing connection took 30 seconds, depending how long the underlying connection was inactive. (the underlying protocol has a timeout you can configure)

Our solution was to clean up any handled events and dereference the hub connection object - and then just start fresh with a new instance after the app is reactivated. This works reliably and is almost instant. The server disconnect event fires correctly as well, although it is fired for each instance that disconnects.

I've documented our journey and solution in this post Best practice for reconnecting SignalR 2.0 .NET client to server hub. Hope it is helpful.

like image 45
Ender2050 Avatar answered Nov 17 '22 00:11

Ender2050