Ask coding questions

← Back to all posts
How can you detect if someone has disconnected from a flask server?
BenjaminMacDou1 (1)

I am using this flask server to store all of the variables, and I am using a separate python file to run the actual battleship games. I am using requests to get the values I need. However, I need a way to determine in the server if somebody has closed the tab for the other file, stopped the repl, etc. and close that room in the server. Thanks in advance.

Answered by btfuss (162) [earned 5 cycles]
View Answer
Comments
hotnewtop
btfuss (162)

I don't know if this is the answer to your question, but: https://stackoverflow.com/questions/14886400/checking-if-a-user-disconnects-using-flask - You can also constantly ping a site or repl to check if the server is online. If the flask server is offline, replit may add their page which says so, and you can check if the html code is listed.

BenjaminMacDou1 (1)

@btfuss I was thinking about trying to figure out how to have a time limit before it thinks that a user has disconnected but I didn’t really know how or if it could be done.

btfuss (162)

@BenjaminMacDou1 So, you know how @Coder100 mentioned the sockets? Hook that up to a node.js server or something, and make the socket ping the server. If the server detects the socket to stop pinging, make a countdown in an array for that user, and if they don't log on after the countdown, do whatever! Another possibility is python socket.engine or socket-io (requires pip) (https://github.com/0xJeremy/socket.engine/blob/master/python/docs/HostClient_Documentation.md - socket.engine) which can make host/client connections. You can run two python files at a time using bash (https://unix.stackexchange.com/questions/582092/how-can-i-run-multiple-bash-scripts-simultaneously-in-a-terminal-window). Against the before, I would use node.js for a client and server side. The server would be on the same side as the flask because they would run at the same time (using bash), so they could listen in and function as a webpage. The downside to this is you need this repl to be on 24/7 which requires the $7 a month hacker plan which is worth it. If you still follow through with this, you can set up the client side with another node.js extension or something. Overall, the best solution would be to use javascript (https://stackoverflow.com/questions/147636/best-way-to-detect-when-a-user-leaves-a-web-page), which if you have the flask HTML file pair up with, can run a command the second a user leaves the page. This doesn't require any plan nor socket communication, just a connection to javascript. I hoped some of these resources help, feel free to send me any questions!

i spent time on this pls give mark lol

BenjaminMacDou1 (1)

@btfuss Wow that's a lot. Thanks! I think that using the python socket engine would work the best for me if it works.

btfuss (162)

@BenjaminMacDou1 It's required information.

thanks btw

BenjaminMacDou1 (1)

@btfuss It is giving me an error. It says that in the module, it tries to import cv2, but that it doesn't exist. I also did the pip install thing, so I don't know what's wrong.

BenjaminMacDou1 (1)

@btfuss Actually, I think that using the socket-io module is even better than the socket engine module. It looks like it will work the exact same as the flask socket-io. And it works when I import it.

Coder100 (17479)

you should use flask socketio to do this! Not only will it be what you want, its instantaneous, and sane.

https://flask-socketio.readthedocs.io/en/latest/index.html

BenjaminMacDou1 (1)

@Coder100 I saw that but I thought that it would only be possible if I was using the server as an actual website instead of just a way to store variables. Do you know if it is possible to use sockets in a regular python file(as well as the server)?