0
0
Flaskframework~20 mins

Flask-SocketIO setup - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask-SocketIO Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a client connects in Flask-SocketIO?
Given this Flask-SocketIO server code, what will be printed when a client connects?
Flask
from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
socketio = SocketIO(app)

@socketio.on('connect')
def handle_connect():
    print('Client connected')

if __name__ == '__main__':
    socketio.run(app)
AThe server prints 'Client connected' only when the server starts.
BThe server prints 'Client connected' once a client establishes a connection.
CThe server prints 'Client connected' every time a message is received.
DThe server never prints anything because the event handler is incorrect.
Attempts:
2 left
💡 Hint
Think about what the 'connect' event means in SocketIO.
📝 Syntax
intermediate
1:30remaining
Which code correctly initializes Flask-SocketIO with async mode?
Choose the correct way to initialize Flask-SocketIO with 'eventlet' async mode.
Asocketio = SocketIO(app, async_mode='eventlet')
Bsocketio = SocketIO(app, async='eventlet')
Csocketio = SocketIO(app, mode='eventlet')
Dsocketio = SocketIO(app, asyncMode='eventlet')
Attempts:
2 left
💡 Hint
Check the exact parameter name for async mode in Flask-SocketIO.
state_output
advanced
2:30remaining
What is the output after emitting a message to all clients?
Given this code snippet, what will clients receive?
Flask
from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
socketio = SocketIO(app)

@socketio.on('message')
def handle_message(msg):
    socketio.emit('response', {'data': msg['text']})

if __name__ == '__main__':
    socketio.run(app)
AAll connected clients receive an event 'response' with the same 'text' data sent by one client.
BOnly the client who sent the message receives the 'response' event.
CNo clients receive any event because 'emit' is missing a target.
DThe server crashes because 'msg' is not defined.
Attempts:
2 left
💡 Hint
By default, socketio.emit sends to all clients.
🔧 Debug
advanced
2:00remaining
Why does this Flask-SocketIO server fail to start?
Identify the error in this code preventing the server from running.
Flask
from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
socketio = SocketIO

if __name__ == '__main__':
    socketio.run(app)
AThe run method is called incorrectly with wrong parameters.
BFlask app is not created correctly, causing TypeError.
CMissing import for eventlet causes runtime error.
Dsocketio is assigned to the class SocketIO, not an instance, causing AttributeError.
Attempts:
2 left
💡 Hint
Check how socketio is assigned before calling run.
🧠 Conceptual
expert
3:00remaining
What is the role of the 'namespace' parameter in Flask-SocketIO event handlers?
Select the correct explanation of the 'namespace' parameter in Flask-SocketIO event decorators.
AIt sets the security level for the event, restricting access to authenticated clients only.
BIt defines the Python module where the event handler is located.
CIt specifies a communication channel to separate events logically, allowing multiple independent event groups.
DIt determines the order in which event handlers are executed on the server.
Attempts:
2 left
💡 Hint
Think of namespaces like different chat rooms or channels.