Search code examples
javascriptpythonflaskwebflask-socketio

(Flask) how to detect client disconnect in flask sockets


I want to print something when a client disconnected, but this isn't working

Javascript client:

socket.on("connect", () => {
   
    socket.on("disconnect",  () => {
     
       socket.emit("disconnected", "{{room_num}}")

   })

})

Python flask :

@socketio.on("disconnected")
def handle_disconnect(room_num):
   print(f"user in room {room_num} disconnected")

Solution

  • Turns out its impossible to emit something when disconnected to I just ended up using request.sid to identify different disconnected clients.