0
0
Flaskframework~10 mins

Broadcasting to clients 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'
ASocketIO
BFlaskSocket
CSocket
DFlaskIO
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like FlaskSocket or FlaskIO.
Trying to import Socket instead of SocketIO.
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
DSocketServer
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like Socket or SocketServer.
Not passing the Flask app instance to the SocketIO constructor.
3fill in blank
hard

Fix the error in the event handler to broadcast a message to all clients.

Flask
@socketio.on('message')
def handle_message(msg):
    socketio.emit('message', msg, [1]=True)
Drag options to blanks, or click blank then click option'
Asend
Bbroadcasting
Csend_all
Dbroadcast
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect argument names like broadcasting or send_all.
Omitting the broadcast argument and sending only to the sender.
4fill in blank
hard

Fill both blanks to emit a custom event to all clients except the sender.

Flask
@socketio.on('chat')
def handle_chat(data):
    socketio.emit([1], data, [2]=True)
Drag options to blanks, or click blank then click option'
A'chat_response'
B'chat_message'
Cbroadcast
Dinclude_self
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong event names like 'chat_response'.
Using include_self instead of broadcast.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that broadcasts only messages longer than 5 characters.

Flask
messages = {user: msg for user, msg in data.items() if len(msg) [1] [2]
socketio.emit('filtered', messages, [3]=True)
Drag options to blanks, or click blank then click option'
A>
B5
Cbroadcast
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than operator instead of greater than.
Forgetting to set broadcast=True in emit.