Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q7 of 15
Flask - WebSocket and Real-Time
What is wrong with this code snippet?
@socketio.on('message')
def handle_message(data):
    room = data['room']
    emit('message', data['msg'])
    join_room(room)
Ajoin_room should be called before emit to send message to the room
Bjoin_room cannot be called inside event handlers
Cdata['msg'] is not a valid key
Demit is missing the room parameter to send message to the room
Step-by-Step Solution
Solution:
  1. Step 1: Check emit call

    emit sends message but lacks room=room, so message goes to sender only.
  2. Step 2: Understand join_room timing

    join_room after emit does not affect this emit call.
  3. Final Answer:

    emit is missing the room parameter to send message to the room -> Option D
  4. Quick Check:

    emit needs room= to target room [OK]
Quick Trick: Add room= to emit to send to room [OK]
Common Mistakes:
MISTAKES
  • Thinking join_room order affects emit already called
  • Assuming data['msg'] is invalid
  • Believing join_room cannot be called in handlers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes