Bird
0
0

Given this Flask-SocketIO code snippet:

medium📝 component behavior Q4 of 15
Flask - WebSocket and Real-Time
Given this Flask-SocketIO code snippet:
from flask_socketio import SocketIO
socketio = SocketIO(app)

@socketio.on('message')
def handle_message(msg):
    socketio.emit('response', msg, broadcast=True)

What happens when a client sends a 'message' event with data 'Hi'?
AOnly the sender client receives a 'response' event with data 'Hi'
BAll connected clients receive a 'response' event with data 'Hi'
CNo clients receive any event
DAn error occurs because broadcast=True is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the event handler

    When 'message' event is received, the server emits 'response' with the same message to all clients because broadcast=True.
  2. Step 2: Understand broadcast=True effect

    Broadcast=True sends the event to all connected clients, not just the sender.
  3. Final Answer:

    All connected clients receive a 'response' event with data 'Hi' -> Option B
  4. Quick Check:

    broadcast=True sends to all clients [OK]
Quick Trick: broadcast=True sends to all clients, not just sender [OK]
Common Mistakes:
MISTAKES
  • Thinking only sender gets the message
  • Believing broadcast=True causes error
  • Assuming no event is sent

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes