Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically restore/reconnect SignalR connection when website restarts

I have a web site written in ASP.net that uses SignalR.

Whenever I publish out changes to the web servers, any open SignalR connections get disconnected without warning, so it appears to users that it's just frozen. Of course, if they refresh the page, the connection is restored and they can go on.

Is there a way to gracefully re-open those lost connections, without requiring the user to act in any way?

like image 213
Andrew Simpson Avatar asked Oct 11 '14 19:10

Andrew Simpson


1 Answers

If your website/appdomain restarts within 30 seconds (SignalR's default DisconnectTimeout), SignalR should try to automatically and seamlessly reestablish the connection.

The SignalR client will trigger a reconnecting event when it notices the server has gone down and starts attempting to reconnect. It will then fire a reconnected event if it succeed to reconnect before the DisconnectTimeout.

On the server, the OnReconnected event will be called on the Hub or PersistentConnection the client successfully reconnects to automatically. This event will be called in the updated web app.

If you find that clients are disconnecting, this likely means they were unable to reconnect before the DisconnectTimeout expired. You can either expand the DisconnectTimeout, or you can attempt to manually start a new connection after the client becomes disconnected. If you manually start a new connection OnConnected would be called on the server instead of OnReconnected and the client would be given a new ConnectionId.

More details about SignalR's reconnect logic and how you can manually restart the connection on the SignalR client can be in this guide for "Understanding and Handling Connection Lifetime Events in SignalR".

like image 57
halter73 Avatar answered Sep 20 '22 11:09

halter73