What causes the 'disconnect' event, exactly? Is there a timeout to disconnect clients that is very short? I have found that I get disconnect for clients while they are still on my page- and only a few seconds after they originally navigate to my page.
@ShermanMorrison the client and server exchange heart beat messages while the connection is active. When the server stops receiving these messages it will declare the client disconnected. The client can also disconnect explicitly.
What you are experiencing though is probably the first case. The client has a retry logic so whenever the connection is dropped it'll try to reconnect. I'm not sure why this is happen, you may want to look at the network tab in your browser's console to see what's happening at the request/response level.
It seems problematic that there are these two very different ways to trigger disconnect messages.
I want to only drop users from a server-side list if they disconnect because they (A) did not send a heartbeat to the server in the specified interval. However, there is a problem because clients can (B) disconnect explicity by closing the page.
My instance of the problem occurs when the client refreshes the page (lobby.html). This disconnects the socket (@socketio.on('disconnect', namespace=..) triggers), and this occurs after the connect message triggers (@socketio.on('connect', namespace=..)).
I suppose I could introduce a delay before calling io.connect, so that the 'connect' event would be received by the server after the 'disconnect' event.
But this is really unwanted behavior on top of unwanted behavior. What I really want is to tell distinctly when the server misses a heartbeat from a client. Can I currently do this with Flask-Socketio? Or would I have to write my own heartbeat system on top of Flask-Socketio by sending messages at intervals in JavaScript, and checking for updates at intervals in Python on the server?
What I really want is to tell distinctly when the server misses a heartbeat from a client.
Well, this is really when your disconnect even is called.
I think the problem that you have with refresh is that the new connection (started by the refreshed page) is received before the heartbeat for the old connection times out. You will have this problem even if you implement your own high level heartbeat, so you'll have to allow this situation that will have two entries for a client for a short interval of time.
I'm not sure there is a good way to address this, since you have to give the heartbeats some time to arrive in case they get delayed for an unrelated reason.
Thank you. That really cleared things up for me- I'll just be content with briefly having 2 or more entries for a client upon refreshes.
is it possible to use disconnect when the user exits the website itself and not just the webpage
i
@Jaysins The web browser will close the WebSocket connection when you exit the website and go somewhere else, or close the window or tab. For long-polling connections, the server will wait for a bit, and then apply a timeout to the client and disconnect it.
@miguelgrinberg i want to call a logout function when the user exits the web browser,, you know sometimes people forget to logout and just close the browser,,, i want as he's tryna exit he'll be disconnected and logged out
@miguelgrinberg thanks for replying tho
Okay. You can then use a callback in your <body> tag:
<body onbeforeunload="my_disconnect_callback();">
The browser will call your callback function when the user closes the tab or window.
@miguelgrinberg the disconnect function is in the flask code`,,not in the template, will callback still work?
p,s i started learning flask lastweek x_x
Consider that this is a cllent-server architecture. The client has a disconnect callback, and the server has a disconnect callback. The onbeforeunload callback is client-side. Your JavaScript callback will then issue a SocketIO disconnect, which will trigger the server to issue its own disconnect callback.
Also keep in mind what I said above. The client will disconnect on its own, even if you don't set up a client-side callback.
thanks alot @miguelgrinberg really cleared everything up for me!
Most helpful comment
@Jaysins The web browser will close the WebSocket connection when you exit the website and go somewhere else, or close the window or tab. For long-polling connections, the server will wait for a bit, and then apply a timeout to the client and disconnect it.