0
0
Flaskframework~10 mins

WebSocket events handling 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 class.

Flask
from flask_socketio import [1]
Drag options to blanks, or click blank then click option'
AFlaskSocket
BFlaskIO
CSocket
DSocketIO
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'FlaskSocket' instead of 'SocketIO'.
Trying to import from 'flask_socket' instead of 'flask_socketio'.
2fill in blank
medium

Complete the code to create a SocketIO instance with the Flask app.

Flask
socketio = [1](app)
Drag options to blanks, or click blank then click option'
ASocket
BSocketIO
CFlaskSocketIO
DFlaskSocket
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name that does not exist like 'Socket' or 'FlaskSocket'.
Forgetting to pass the Flask app instance.
3fill in blank
hard

Fix the error in the event handler decorator to listen for 'message' events.

Flask
@socketio.on('[1]')
def handle_message(msg):
    print('Received:', msg)
Drag options to blanks, or click blank then click option'
Amessage
Bdisconnect
Cconnect
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' or 'disconnect' which are for connection events, not messages.
Using 'send' which is a method, not an event name.
4fill in blank
hard

Fill both blanks to emit a 'response' event with data back to the client.

Flask
socketio.[1]('[2]', {'data': 'Hello client!'})
Drag options to blanks, or click blank then click option'
Aemit
Bsend
Cresponse
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of emit for custom events.
Using 'message' as event name instead of 'response'.
5fill in blank
hard

Fill all three blanks to define a handler for client connection and send a welcome message.

Flask
@socketio.on('[1]')
def [2]():
    socketio.[3]('welcome', {'msg': 'Welcome!'})
Drag options to blanks, or click blank then click option'
Aconnect
Bhandle_connect
Cemit
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'message' instead of 'connect' for the decorator.
Naming the handler function incorrectly or forgetting it.
Using 'send' instead of 'emit' to send the welcome message.