Bird
0
0

Given this Flask-SocketIO event handler:

medium📝 component behavior Q5 of 15
Flask - WebSocket and Real-Time
Given this Flask-SocketIO event handler:
@socketio.on('exit')
def on_exit(data):
    room = data['room']
    leave_room(room)
    emit('status', f'User exited {room}', room=room)

What is the effect when a client triggers the 'exit' event?
AThe client leaves the specified room and all clients in that room receive the 'status' message.
BThe client leaves the room but no message is sent to other clients.
CThe client remains in the room but receives a confirmation message.
DThe client leaves the room and only the client receives the 'status' message.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze leave_room(room)

    This function removes the client from the specified room.
  2. Step 2: Analyze emit call

    The emit sends a 'status' event with a message to all clients in the room specified by room=room.
  3. Step 3: Understand message recipients

    Since the client has left the room before emitting, the message is sent to all remaining clients in that room, excluding the sender.
  4. Final Answer:

    The client leaves the room and all other clients in that room receive the message. -> Option A
  5. Quick Check:

    leave_room removes client; emit with room sends to room members. [OK]
Quick Trick: leave_room removes client; emit with room sends to room members [OK]
Common Mistakes:
MISTAKES
  • Assuming the sender receives the message after leaving the room
  • Thinking no message is sent after leave_room
  • Confusing emit's room parameter behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes