C
C#12mo ago
msr

✅ 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
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
you didn't read the 2nd half of my comment that is explicitly "not" the behavior I am seeing
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
export const connection = new HubConnectionBuilder()
.withUrl(url)
.withAutomaticReconnect()
.build();
export const connection = new HubConnectionBuilder()
.withUrl(url)
.withAutomaticReconnect()
.build();
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 loop
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
this makes sense in the context of what i was experiencing
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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 af
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
for the SPA, yes
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
if they stay within the SPA, it is perfectly controlled and everything works
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
if they close the tab/browser, i'm also seeing connection drop immediately
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
that makes sense
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
yikes... yeah that makes 100% perfect sense
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
thats what I was finding when I went down the road of blocking navigation
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
i'm not terribly familiar with oidc, so that goes a bit over my head
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
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
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
msr
msrOP12mo ago
hm.. i think I am using both add auth and add negotiate. i will explore that and see what I can find
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server