I'm using ASP.NET MVC 5 and using SignalR v2.2.2 (the latest). My application is working all okay until the page has been idle for a while (not sure exactly now long but between 5 to 15 mins idle). Once it has been idle for this period of time, the buttons on the web page no longer work (as I assume they need an active connection).
My preferred way to solve this is to be able to increase the timeout of the connection. How can I do this?
If this is not possible, how can I detect that the connection has been dropped on the page and/or how can I re-establish connection if required?
My web page uses javascript and the code that establishes the connection to the SignalR hub is this:
$.connection.hub.start()
.done(function () {
// Does stuff here...
})
.fail(function (e) { console.log(e); });
Thank you
I'm using ASP.NET MVC 5 and using SignalR v2.2.2 (the latest). My application is working all okay until the page has been idle for a while (not sure exactly now long but between 5 to 15 mins idle). Once it has been idle for this period of time, the buttons on the web page no longer work (as I assume they need an active connection).
My preferred way to solve this is to be able to increase the timeout of the connection. How can I do this?
If this is not possible, how can I detect that the connection has been dropped on the page and/or how can I re-establish connection if required?
My web page uses javascript and the code that establishes the connection to the SignalR hub is this:
$.connection.hub.start()
.done(function () {
// Does stuff here...
})
.fail(function (e) { console.log(e); });
Thank you
Share Improve this question edited Nov 23, 2020 at 21:20 hardkoded 21.8k3 gold badges61 silver badges74 bronze badges asked Feb 12, 2018 at 15:11 milliemillie 2,90511 gold badges43 silver badges63 bronze badges 3- The network tab in your browser console should give you some more info, but I suspect the server application is sleeping, which has nothing to do with connection timeouts. I'm guessing you're on IIS? If so, what is triggering the events that you want to send to your front end? – Reinstate Monica Cellio Commented Feb 12, 2018 at 15:15
- The docs have everything you need - learn.microsoft./en-us/aspnet/signalr/overview/… – maurcz Commented Feb 12, 2018 at 19:23
- The question is old but would like to know the approach you took to fix this? I am having a similar issue, chrome suspend the tab after few minutes. – GKG4 Commented Aug 15, 2020 at 15:58
3 Answers
Reset to default 3There are 2 recipes you can implement:
1.Reconnect if the connection drops:
$.connection.hub.disconnected(function () {
setTimeout(function () {
$.connection.hub.start();
}, 5000);
});
2.Use the pingInterval
so you try to keep the connection alive:
$.connection.hub.start({ pingInterval: 6000 })
There's a much simpler way now:
.withAutomaticReconnect()
See this post
From Microsoft Docs,
Handle the connectionSlow event to display a message as soon as SignalR is aware of connection problems, before it goes into reconnecting mode.
Handle the reconnecting event to display a message when SignalR is aware of a disconnection and is going into reconnecting mode.
Handle the disconnected event to display a message when an attempt to reconnect has timed out. In this scenario, the only way to re-establish a connection with the server again is to restart the SignalR connection by calling the Start method, which will create a new connection ID.