Complete the code to import the Flask-SocketIO extension.
from flask_socketio import [1]
The Flask-SocketIO extension is imported using SocketIO.
Complete the code to initialize SocketIO with the Flask app.
app = Flask(__name__)
socketio = [1](app)We initialize SocketIO by calling SocketIO(app) to bind it to the Flask app.
Fix the error in the event handler to join a room.
@socketio.on('join') def on_join(data): username = data['username'] room = data['room'] [1](room) send(f"{username} has entered the room.", room=room)
The correct function to join a room is join_room(room) from Flask-SocketIO.
Fill both blanks to send a message to a specific room and leave it.
@socketio.on('leave') def on_leave(data): username = data['username'] room = data['room'] send(f"{username} has left the room.", [1]=room) [2](room)
To send a message to a room, use send(..., room=room). To leave a room, call leave_room(room).
Fill all three blanks to emit a message event to a room with JSON data.
@socketio.on('message') def handle_message(data): room = data['room'] message = data['msg'] [1]('new_message', {'msg': message}, [2]=room, [3]=False)
send instead of emit for custom events.broadcast to True which sends to all clients.Use emit to send an event. Specify the room keyword to target the room, and broadcast=False to avoid sending to all clients.
