28

I am developing a real-time client-server application using SignalR, ASP.NET and C#. I am using localhost as host and VS2013.

My questions are:

  1. Why if I close server, on web-client the "Reconnect" event occurs?

  2. The "Disconnect" event occurs after 40+ seconds only. How to reduce this time?

  3. I need the client to connect to server on start. The "Reconnect" event should occurs within fixed interval only. If "Reconnect" interval time is over the client should connect as a new client. How to archive this goal?

Finally, I would like to ask - how to keep alive connection using SignalR in the right way?

I am using this code:

C#

public override Task OnDisconnected()
{
clientList.RemoveAt(nIndex);
Console.WriteLine("Disconnected {0}\n", Context.ConnectionId);
return (base.OnDisconnected());
}

public override Task OnReconnected()
{
Console.WriteLine("Reconnected {0}\n", Context.ConnectionId);
return (base.OnReconnected());
}

Javascript

$.connection.hub.reconnected(function () {
// Html encode display name and message.
var encodedName = $('<div />').text("heartbeat").html();
var now = new Date();

// Add the message to the page.
$('#discussion').append('Reconnected to server: ' + now + '</br>');
});

$.connection.hub.disconnected(function () {
// Html encode display name and message.
var encodedName = $('<div />').text("heartbeat").html();
var now = new Date();
// Add the message to the page.
$('#discussion').append('Disconnected from server: ' + now + '</br>');
});

After Connect output:

message received from server : Fri Feb 21 2014 10:53:02

After Close Server output:

Reconnected to server: Fri Feb 21 2014 10:53:22 <-- Why, if i close server ???
Disconnected from server: Fri Feb 21 2014 10:53:53 <-- Why 40+ seconds after the server is closed ?

2 Answers 2

32

1. After I close server, on web-client the "Reconnect" event occurs and the "Disconnect" event occurs only after. Why?

SignalR cannot tell the difference between closing the server and restarting the server. For this reason, when the server shuts down the client will start to try to reconnect in case the server is actually restarting.

2. The "Disconnect" occurs 30+ seconds after of unknown "Reconnect". How to reduce this time?

This 30 second timeout can be modified via the DisconnectTimeout property.

3. I need the client to connect to the server on start. The "Reconnect" should occurs within fixed interval only. If "Reconnect" interval time is over the client should connect as new client.

You should start the connection on the disconnected event, preferably after a timeout to reduce server load if it restarts.

$.connection.hub.disconnected(function() {
    setTimeout(function() {
        $.connection.hub.start();
    }, 5000); // Re-start connection after 5 seconds
});

The entire Understanding and Handling Connection Lifetime Events in SignalR article is probably relavent to your question.

5
  • But after you are in the Disconnect state, don't you lose all your subscribed events on the client side? How can you recreate those subscriptions (e.g. the .On<T>(...) extension)?
    – Killnine
    Nov 24, 2014 at 22:40
  • 1
    Subscriptions should continue to work after you restart a disconnected connection as long as you are still using the original (Hub)Connection object.
    – halter73
    Nov 25, 2014 at 1:10
  • 1
    @halter73 I think you would have to re- subscribe to the groups you were in earlier. Oct 30, 2015 at 18:07
  • 2
    @ShyamalParikh Correct. You will need to resubscribe to groups if you manually restart the connection. I was merely pointing out you don't have to configure you callbacks via .On<T>(...) again if you use Connnection objection.
    – halter73
    Nov 3, 2015 at 18:20
  • There is no event that triggers when the signalR Re-Started? so that the client doesnt need to keep pinging for new connection in set time out?
    – Saravana
    May 19, 2022 at 7:39
3

This method is for when you do not want to change the server configure ; javascript example code :

connection.serverTimeoutInMilliseconds = 1000 * 60 * 10; // for  10 minute
1
  • 1
    Thanks, @SyeedMasoudTayefi. Which connection object is this? Is it the $.connection or is it the connection proxy $.connection.myHub.connection?
    – Alex
    Jun 1, 2021 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.