SignalR Client Disconect

Viewed 222

If I say close the lid on my laptop for a few hours and I am running an application using SignalR, then the server Hub tries to send a message to that connectionId what happens, will a get an exception from my SendMessage? When I open my laptop lid will my connectionId still be valid and the same value?

2 Answers

In addition to eglease's answer, it matters what version you are running and what browser. If you are using ASP.NET SignalR client 2.4.1 or earlier and Chrome as the browser you will have a 5 minute window before Chrome terminates you because the connection is not actively being used. Same would occur if you just opened another tab and your client was on a unfocused tab.

This was fixed with 2.4.2 release back in April to prevent the issue.

This depends on what happens with your laptop when you close the lid.

If you have presentation mode or sleep disabled, nothing should happen. The client will continue working as before and receiving messages. ConnectionID would not change.

If you laptop goes to sleep and/or disables networking or the SignalR client stops running, the server will get a disconnect after a timeout and will try to reconnect. The client will not receive any messages that it missed unless you have your own logic to monitor what messages were received and what need to be resend. There should be no exception on the application level but if you look at the SignalR trace, there will be an exception within the library itself. This is normal.

ConnectionID depends on how long it takes for the client to come back. It will stay the same for a short period of time if OnReconnect() works. If the disconnect is longer and OnDisconnect() is triggered, the ConnectionID will be different because the client will need to reconnect manually (new connection).

Note: SignalR for ASP.NET does have some resend and buffer logic but it does not buffer much and does not work well. For this reason, it was removed in SignalR for .NET Core.

If you need the resend functionality, take a look at NATS.io.

TLDR; No exception but new CustomerID if the disconnect period is long enough for OnDisconnect() to trigger on the server.

Related