0
0
Flaskframework~10 mins

Room-based messaging in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Flask-SocketIO extension.

Flask
from flask_socketio import [1]
Drag options to blanks, or click blank then click option'
ASocket
BSocketIO
CFlaskSocket
DFlaskIO
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like FlaskSocket or FlaskIO.
Trying to import from the wrong module.
2fill in blank
medium

Complete 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'
ASocketIO
BSocket
CFlaskSocketIO
DSocketApp
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like Socket or SocketApp.
Not passing the Flask app instance.
3fill in blank
hard

Fix 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'
Ajoinroom()
BjoinRoom
Cjoinroom
Djoin_room
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase or incorrect casing like joinRoom.
Calling a non-existent function joinroom.
4fill in blank
hard

Fill 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'
Aroom
Bleave_room
Cbroadcast
Dsend_room
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keyword arguments like broadcast instead of room.
Calling a non-existent function like send_room.
5fill in blank
hard

Fill 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'
Aemit
Broom
Cbroadcast
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of emit for custom events.
Setting broadcast to True which sends to all clients.