Barguast
Barguast

Reputation: 6196

SignalR - storing client information

I've started experimenting with SignalR. I've been trying to come up with a flexible way of storing information about each connected client. For example, storing the name in a chat app rather than passing it with each message.

At the moment, I have a static dictionary which matches the connectionId to an object which contains these properties. I add to this dictionary on connection, and remove on disconnection.

The issue I'm having is that I don't seem to get all disconnect events. If I close a tab in Chrome, the disconnect seems to go through. However, if I rapidly reload a tab, the disconnect doesn't seem to occur (at least not 'cleanly'). For example, if I reload the same tab over and over, it'll tell me my dictionary has multiple items when it should - in theory still be one.

Is there a standard way of storing this kind of per-connection information? Otherwise, what might be causing the issue I'm having?

Upvotes: 2

Views: 763

Answers (1)

N. Taylor Mullen
N. Taylor Mullen

Reputation: 18301

You are actually handling connection id data correctly. Ensure that you are only instantiating your user data in OnConnected and uninstantiating it in OnDisconnected.

When spamming refresh on your page there are situations which result in the OnDisconnected event not being triggered immediately. However you should not worry about this because SignalR will actually time-out the connection and trigger the OnDisconnected event after a designated timeout (DisconnectTimeout).

If you do come across scenarios where there is not a 1-to-1 correlation for OnConnected and OnDisconnected events (after a significant amount of time) make sure to file a bug at https://github.com/SignalR/SignalR/issues.

Lastly if you're looking at doing some advanced chat mechanics and looking for some inspiration check out JabbR, it's open source!

https://github.com/davidfowl/JabbR

Hope this helps!

Upvotes: 1

Related Questions