Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask-SocketIO extension.
Flask
from flask_socketio import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like FlaskSocket or FlaskIO.
Trying to import from the wrong module.
✗ Incorrect
The Flask-SocketIO extension is imported using SocketIO.
2fill in blank
mediumComplete the code to initialize SocketIO with the Flask app.
Flask
app = Flask(__name__)
socketio = [1](app) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like Socket or SocketApp.
Not passing the Flask app instance.
✗ Incorrect
We initialize SocketIO by calling SocketIO(app) to bind it to the Flask app.
3fill in blank
hardFix the error in the event handler to join a room.
Flask
@socketio.on('join') def on_join(data): username = data['username'] room = data['room'] [1](room) send(f"{username} has entered the room.", room=room)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase or incorrect casing like joinRoom.
Calling a non-existent function joinroom.
✗ Incorrect
The correct function to join a room is join_room(room) from Flask-SocketIO.
4fill in blank
hardFill both blanks to send a message to a specific room and leave it.
Flask
@socketio.on('leave') def on_leave(data): username = data['username'] room = data['room'] send(f"{username} has left the room.", [1]=room) [2](room)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keyword arguments like broadcast instead of room.
Calling a non-existent function like send_room.
✗ Incorrect
To send a message to a room, use send(..., room=room). To leave a room, call leave_room(room).
5fill in blank
hardFill all three blanks to emit a message event to a room with JSON data.
Flask
@socketio.on('message') def handle_message(data): room = data['room'] message = data['msg'] [1]('new_message', {'msg': message}, [2]=room, [3]=False)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
send instead of emit for custom events.Setting
broadcast to True which sends to all clients.✗ Incorrect
Use emit to send an event. Specify the room keyword to target the room, and broadcast=False to avoid sending to all clients.