Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React to SignalR keep-alive ping

Tags:

signalr

Can a SignalR server application react to the keep-alive pings used internally to SignalR?

I'm working on an application that will perform expensive queries for signalr clients. The queries will not be executed on the same machine as the one the server is connected to. The queries are expensive to run, and easy to restart, therefore the workers doing the work don't want to be doing work for a disconnected client or for an unreachable or down server; they want assurance the client is still there. This could be trivially accomplished by simply having the client ping the web server at some interval, and the web server could then pass the ping along to the worker.

public class PingedHub : Hub
{
    private readonly IPingListener _listener;

    public Hub(IPingListener listener)
    {
        _listener = listener;
    }

    public Ping()
    {
        _listener.Ping(Context.ConnectionId);
    }
}

SignalR already does this. In order to detect lost connections, SignalR sends a Keep-Alive ping if no other traffic is transmitted in a time period, configured by GlobalHost.Configuration.KeepAlive. If it doesn't receive any traffic in a larger period, configured by GlobalHost.Configuration.ConnectionTimeout, it disconnects the client and raises OnDisconnected.

Is it possible to hook into the existing system to get positive assurance that a client is connected, or do clients need to send a redundant ping?

like image 954
Cirdec Avatar asked Feb 23 '26 13:02

Cirdec


1 Answers

There's no need to hook into the SignalR connection system. The pings will not be redundant; if your client sends pings at least as often as GlobalHost.Configuration.KeepAlive, SignalR won't send its own keep-alive pings.

If you did find a way to hook into the SignalR system, you'd be faced with a new problem. Keep-alive pings aren't sent when there's other traffic, but your workers will still want to know that the client's still alive. If the communication channel is active for some other reason, you'd have to choose between spamming the workers with pings, or having some intermediate system in place to throttle them.

like image 71
Cirdec Avatar answered Feb 27 '26 01:02

Cirdec