0
0
Flaskframework~20 mins

Server-Sent Events alternative in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding WebSocket as an alternative to Server-Sent Events

Which statement best describes why WebSocket can be a good alternative to Server-Sent Events (SSE) in Flask applications?

AWebSocket is a polling technique that repeatedly requests data from the server.
BWebSocket only supports one-way communication from server to client, similar to SSE.
CWebSocket allows two-way communication between client and server, unlike SSE which is one-way from server to client.
DWebSocket requires the client to refresh the page to receive new data.
Attempts:
2 left
💡 Hint

Think about the direction of data flow in WebSocket compared to SSE.

component_behavior
intermediate
2:00remaining
Flask WebSocket server behavior

Given a Flask app using Flask-SocketIO, what will happen when the server emits a message to a connected client?

Flask
from flask import Flask
from flask_socketio import SocketIO, emit

app = Flask(__name__)
socketio = SocketIO(app)

@socketio.on('connect')
def handle_connect():
    emit('message', {'data': 'Welcome!'})
AThe client will receive the message only after refreshing the page.
BThe client must send a message first before receiving any data from the server.
CThe server will raise an error because emit cannot be called inside the connect event.
DThe client receives the 'Welcome!' message immediately after connecting without needing to request it.
Attempts:
2 left
💡 Hint

Consider what happens when a client connects to a WebSocket server.

📝 Syntax
advanced
2:00remaining
Correct Flask WebSocket event handler syntax

Which option shows the correct syntax for a Flask-SocketIO event handler that listens for a 'chat message' event and sends a response?

A
@socketio.on('chat message')
def handle_message(msg):
    emit('response', {'data': msg})
B
@socketio.on_event('chat message')
def handle_message(msg):
    emit('response', {'data': msg})
C
@socketio.event('chat message')
def handle_message(msg):
    emit('response', {'data': msg})
D
@socketio.listen('chat message')
def handle_message(msg):
    emit('response', {'data': msg})
Attempts:
2 left
💡 Hint

Check the Flask-SocketIO documentation for the correct decorator to listen for events.

🔧 Debug
advanced
2:00remaining
Debugging Flask WebSocket connection issue

A developer uses Flask-SocketIO but clients never receive messages. The server code is:

from flask import Flask
from flask_socketio import SocketIO, emit

app = Flask(__name__)
socketio = SocketIO(app)

@socketio.on('connect')
def on_connect():
    emit('message', {'data': 'Hello'})

if __name__ == '__main__':
    app.run()

What is the main reason clients do not receive messages?

AThe server is started with app.run() instead of socketio.run(app), so WebSocket support is not enabled.
BThe emit call inside the connect event is invalid and causes a runtime error.
CThe client must send a 'connect' event manually before receiving messages.
DThe Flask app is missing a route for '/' which prevents connections.
Attempts:
2 left
💡 Hint

Consider how Flask-SocketIO requires the server to be started.

state_output
expert
3:00remaining
State behavior in Flask WebSocket with multiple clients

Consider a Flask-SocketIO server that keeps a counter of connected clients and broadcasts it on each connection and disconnection:

from flask import Flask
from flask_socketio import SocketIO, emit

app = Flask(__name__)
socketio = SocketIO(app)

connected_clients = 0

@socketio.on('connect')
def on_connect():
    global connected_clients
    connected_clients += 1
    socketio.emit('clients', {'count': connected_clients})

@socketio.on('disconnect')
def on_disconnect():
    global connected_clients
    connected_clients -= 1
    socketio.emit('clients', {'count': connected_clients})

If three clients connect one after another, then one disconnects, what is the sequence of 'count' values broadcasted?

A3, 2, 1, 0
B1, 2, 3, 2
C0, 1, 2, 3
D1, 1, 1, 0
Attempts:
2 left
💡 Hint

Track the value of connected_clients after each event.