0
0
Flaskframework~5 mins

WebSocket events handling in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a WebSocket in Flask?
A WebSocket in Flask is a way to open a two-way communication channel between the client and server. It allows sending and receiving messages instantly without reloading the page.
Click to reveal answer
beginner
Which Flask extension is commonly used for WebSocket support?
Flask-SocketIO is the extension that adds WebSocket support to Flask applications. It helps handle events like connect, disconnect, and message easily.
Click to reveal answer
intermediate
What does the @socketio.on('event_name') decorator do?
It tells Flask-SocketIO to listen for a specific event from the client named 'event_name'. When that event happens, the decorated function runs to handle it.
Click to reveal answer
intermediate
How do you send a message back to the client in Flask-SocketIO?
Use the emit() function inside an event handler. For example, emit('response_event', {'data': 'Hello'}) sends a message named 'response_event' with data to the client.
Click to reveal answer
beginner
What is the purpose of the 'connect' and 'disconnect' events in WebSocket handling?
The 'connect' event runs when a client opens a WebSocket connection. The 'disconnect' event runs when the client closes it. They help manage user sessions and resources.
Click to reveal answer
Which Flask extension is used to handle WebSocket events?
AFlask-SocketIO
BFlask-RESTful
CFlask-WTF
DFlask-Migrate
What decorator do you use to listen for a WebSocket event named 'chat_message'?
A@app.route('/chat_message')
B@socketio.on('chat_message')
C@socketio.emit('chat_message')
D@app.websocket('chat_message')
How do you send a message back to the client in Flask-SocketIO?
Areturn data
Bsend('event', data)
Cemit('event', data)
Dresponse(data)
What event runs when a client closes the WebSocket connection?
Adisconnect
Bclose
Cend
Dstop
Which of these is NOT a WebSocket event in Flask-SocketIO?
Aconnect
Bdisconnect
Cmessage
Dsubmit
Explain how Flask-SocketIO handles WebSocket events from client connection to message sending.
Think about the lifecycle of a WebSocket connection and how events are handled.
You got /5 concepts.
    Describe the role of the @socketio.on decorator and the emit function in WebSocket communication.
    Focus on how Flask-SocketIO listens and responds to events.
    You got /3 concepts.