Bird
0
0

What is wrong with this Flask-SocketIO event handler?

medium📝 Debug Q7 of 15
Flask - WebSocket and Real-Time
What is wrong with this Flask-SocketIO event handler?
@socketio.on('disconnect')
def on_disconnect():
    print('Client disconnected')
    socketio.emit('user_left')
AMissing data parameter causes runtime error
Bprint statement is not allowed in event handlers
CEmitting events on disconnect may fail because client is gone
DDecorator @socketio.on('disconnect') is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Understand disconnect event behavior

    When a client disconnects, sending events to that client is not possible.
  2. Step 2: Identify issue with emitting on disconnect

    Emitting 'user_left' to clients may fail if targeting disconnected client; broadcast should be used carefully.
  3. Final Answer:

    Emitting events on disconnect may fail because client is gone -> Option C
  4. Quick Check:

    Disconnect event: client gone, emitting may fail [OK]
Quick Trick: Avoid emitting to disconnected clients; use broadcast carefully [OK]
Common Mistakes:
MISTAKES
  • Assuming disconnect event can send to disconnected client
  • Expecting data parameter on disconnect event
  • Thinking print is disallowed in handlers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes