Bird
0
0

Given this Flask-SocketIO code snippet, what will happen when a client sends a message to the server?

medium📝 component behavior Q13 of 15
Flask - WebSocket and Real-Time
Given this Flask-SocketIO code snippet, what will happen when a client sends a message to the server?
from flask_socketio import SocketIO, send

socketio = SocketIO(app)

@socketio.on('message')
def handle_message(msg):
    send(f'Received: {msg}', broadcast=True)
AThe server sends back the message only to the sender.
BThe server broadcasts the received message to all connected clients.
CThe server ignores the message and sends no response.
DThe server crashes due to missing return statement.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the @socketio.on decorator

    This listens for 'message' events from clients and triggers handle_message.
  2. Step 2: Analyze the send function with broadcast=True

    send() with broadcast=True sends the message to all connected clients, not just the sender.
  3. Final Answer:

    The server broadcasts the received message to all connected clients. -> Option B
  4. Quick Check:

    send with broadcast=True = broadcast message [OK]
Quick Trick: broadcast=True means send to all clients [OK]
Common Mistakes:
MISTAKES
  • Thinking message goes only to sender
  • Assuming server crashes without return
  • Believing server ignores the message

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes