Challenge - 5 Problems
Flask-SocketIO Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a client connects in Flask-SocketIO?
Given this Flask-SocketIO server code, what will be printed when a client connects?
Flask
from flask import Flask from flask_socketio import SocketIO app = Flask(__name__) socketio = SocketIO(app) @socketio.on('connect') def handle_connect(): print('Client connected') if __name__ == '__main__': socketio.run(app)
Attempts:
2 left
💡 Hint
Think about what the 'connect' event means in SocketIO.
✗ Incorrect
The 'connect' event handler runs each time a client connects, so the print statement executes then.
📝 Syntax
intermediate1:30remaining
Which code correctly initializes Flask-SocketIO with async mode?
Choose the correct way to initialize Flask-SocketIO with 'eventlet' async mode.
Attempts:
2 left
💡 Hint
Check the exact parameter name for async mode in Flask-SocketIO.
✗ Incorrect
The correct parameter is 'async_mode', not 'async' or other variants.
❓ state_output
advanced2:30remaining
What is the output after emitting a message to all clients?
Given this code snippet, what will clients receive?
Flask
from flask import Flask from flask_socketio import SocketIO app = Flask(__name__) socketio = SocketIO(app) @socketio.on('message') def handle_message(msg): socketio.emit('response', {'data': msg['text']}) if __name__ == '__main__': socketio.run(app)
Attempts:
2 left
💡 Hint
By default, socketio.emit sends to all clients.
✗ Incorrect
The emit without specifying 'room' or 'to' sends the event to all connected clients.
🔧 Debug
advanced2:00remaining
Why does this Flask-SocketIO server fail to start?
Identify the error in this code preventing the server from running.
Flask
from flask import Flask from flask_socketio import SocketIO app = Flask(__name__) socketio = SocketIO if __name__ == '__main__': socketio.run(app)
Attempts:
2 left
💡 Hint
Check how socketio is assigned before calling run.
✗ Incorrect
socketio must be an instance of SocketIO, but here it is assigned to the class itself.
🧠 Conceptual
expert3:00remaining
What is the role of the 'namespace' parameter in Flask-SocketIO event handlers?
Select the correct explanation of the 'namespace' parameter in Flask-SocketIO event decorators.
Attempts:
2 left
💡 Hint
Think of namespaces like different chat rooms or channels.
✗ Incorrect
Namespaces allow grouping events so clients can connect to different logical channels independently.