0
0
Flaskframework~20 mins

WebSocket events handling in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a client connects?

Consider this Flask-SocketIO server code snippet. What message will the server send to the client immediately after connection?

Flask
from flask_socketio import SocketIO, emit

socketio = SocketIO()

@socketio.on('connect')
def handle_connect():
    emit('welcome', {'msg': 'Hello, client!'})
AThe client receives no message until it sends a message first
BThe client receives an event 'connect' with data {'msg': 'Hello, client!'}
CThe client receives an event 'welcome' with data {'msg': 'Hello, client!'}
DThe server raises an error because emit is not allowed in connect event
Attempts:
2 left
💡 Hint

Remember that emit sends events to the client. The event name is the first argument.

state_output
intermediate
2:00remaining
What is the value of 'count' after three 'increment' events?

Given this Flask-SocketIO server code, what will be the value of count after the client sends the 'increment' event three times?

Flask
from flask_socketio import SocketIO, emit

socketio = SocketIO()
count = 0

@socketio.on('increment')
def handle_increment():
    global count
    count += 1
    emit('count_updated', {'count': count})
A1
B3
C0
DRaises UnboundLocalError
Attempts:
2 left
💡 Hint

Check how the count variable is updated and the use of global.

📝 Syntax
advanced
2:00remaining
Which option causes a SyntaxError in Flask-SocketIO event handler?

Identify which code snippet will cause a syntax error when defining a Flask-SocketIO event handler.

A
@socketio.on('message')
def handle_message()
    print('Message received')
B
@socketio.on('message')
def handle_message():
    print('Message received')
C
@socketio.on('message')
def handle_message():
    emit('response', {'data': 'ok'})
D
@socketio.on('message')
async def handle_message():
    await some_async_func()
Attempts:
2 left
💡 Hint

Look for missing punctuation or incorrect function definitions.

🔧 Debug
advanced
2:00remaining
Why does the server not respond to 'chat' events?

Given this Flask-SocketIO server code, the client sends 'chat' events but receives no response. What is the likely cause?

Flask
from flask_socketio import SocketIO, emit

socketio = SocketIO()

@socketio.on('message')
def handle_message(data):
    emit('response', {'msg': data['msg']})
AThe function handle_message lacks the global keyword for data
BThe emit call is missing the broadcast=True argument
CThe server must use @socketio.event decorator instead of @socketio.on
DThe event handler listens for 'message' but client sends 'chat', so no handler is triggered
Attempts:
2 left
💡 Hint

Check if the event names match between client and server.

🧠 Conceptual
expert
2:00remaining
What happens if you emit an event outside a SocketIO event context?

In Flask-SocketIO, what is the behavior when you call emit('update', data) outside of any event handler or request context?

AEmit raises a RuntimeError because there is no active SocketIO context
BEmit sends the event to all connected clients automatically
CEmit queues the event and sends it when a client connects
DEmit silently does nothing without error
Attempts:
2 left
💡 Hint

Think about how Flask-SocketIO manages context for emitting events.