✅ SignalR Core Disconnect on Navigate
I'm trying to listen for when a user disconnects so I can remove them from a game lobby.
It seems like OnDisconnectedAsync is called when a user closes the tab/window. But it is not called when they navigate away from the page.
I've tested on Chrome and Edge, and similar behavior. Tested on an older version of the app (also using ASP.NET Core SignalR Core), same behavior.
Is there any way to enforce that it should be called on navigate? I would assume the server should be alerted because the websocket connection would close...
44 Replies
I think this might need to be a client-side concern. I'm not sure why SignalR Core doesn't recognize navigate. In my particular case, I'm using a React SPA, so maybe some react-router magic can intercept a navigation away from the subdomain and do a manual disconnect at that point...
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
I don't know if this is entirely true. The websocket transport is a negotiation between the client and server, it isn't fully a client thing.
The connection is managed on the web server and is kept open because it tries to be robust and handle missed "keep-alive" pings. When a browser closes, it seems like an exception is sent. But on navigation, it seems like it is kept open.
What type of code would need to be written on the server to disconnect eagerly on navigate?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
That isn't my experience.
I have a concurrent dictionary holding references to clients.
OnDisconnectAsync
, I am having that user leave the concurrent dictionary and posting an update to all other users in the same lobby.
When a user force exits the tab/browser, the other clients see an update immediately. When a user navigates away to another site, the update happens 10 seconds later. This behavior is not consistent with your claim that it immediately fails.
Why might I be seeing this behavior?Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
dude... its a dictionary of <id, lobbyInfo> object
it holds other data than the client
but that's not relevant to the question
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
you didn't read the 2nd half of my comment
that is explicitly "not" the behavior I am seeing
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
this is the client code for building a hub connection. There is no custom reconnect behavior happening at all
unless the
.withAutomaticReconnect
is throwing it all for a loopUnknown User•12mo ago
Message Not Public
Sign In & Join Server To View
this makes sense in the context of what i was experiencing
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
so if I understand correctly, I have 2 options:
- intercept navigation on the client side and do a disconnect manually before leaving
- dig into the websocket handshake and change it in some way that allows an eager disconnect on navigate
?
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
thats good to hear, i've been going crazy trying to figure out this problem
it could also be that the eventual consistency of connection cleanup is good enough.
I think using
react-router
to listen for navigation attempts away from the subdomain could be sufficient, even though that code would be ugly afUnknown User•12mo ago
Message Not Public
Sign In & Join Server To View
for the SPA, yes
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
if they stay within the SPA, it is perfectly controlled and everything works
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
on a refresh, we are also cool, connection is persisted
if they leave the SPA and go watch youtube
i want the connection to drop immediately, but I'm seeing what you described, of eventual cleanup
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
if they close the tab/browser, i'm also seeing connection drop immediately
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
that's an interesting point, there is no page unload in a SPA, so I could add an event handler to that browser event and perform a manual disconnect at that point
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
that makes sense
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
yikes... yeah that makes 100% perfect sense
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
thats what I was finding when I went down the road of blocking navigation
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
honestly, because the lobby-state is managed from the concurrent dictionary, it might be sufficient to remove it from there and shoot of an update from the IHubContext to the remaining members of the group
letting the connection id cleanup eventually doesn't matter as long as the current members of the group have the latest version of participant count, sans the guy who left to watch youtube
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
i'm not terribly familiar with oidc, so that goes a bit over my head
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
sorry, had a work meeting.
this particular tool is an internal tool. I'm getting the windows identity user off of NTLM/Negotiate or whatever it is called. The unique SID.
you can't access the tool without being on the enterprise intranet
they wanted to update to use 2 factor auth with a SSO, but it seems a bit annoying to implement, so I went with the deprecated technology
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
its been a unique enterprise experience as none of our products are hosted on the cloud, and all in very closed off on prem/intranet environments
i know very very little about oauth and auth in general
its been challenging to integrate whenever I have needed to leverage it
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
hm.. i think I am using both add auth and add negotiate. i will explore that and see what I can find
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View