Bird
Raised Fist0
Flaskframework~20 mins

Server-Sent Events alternative in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
WebSocket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding WebSocket as an alternative to Server-Sent Events

Which statement best describes why WebSocket can be a good alternative to Server-Sent Events (SSE) in Flask applications?

AWebSocket is a polling technique that repeatedly requests data from the server.
BWebSocket only supports one-way communication from server to client, similar to SSE.
CWebSocket allows two-way communication between client and server, unlike SSE which is one-way from server to client.
DWebSocket requires the client to refresh the page to receive new data.
Attempts:
2 left
💡 Hint

Think about the direction of data flow in WebSocket compared to SSE.

❓ component_behavior
intermediate
2:00remaining
Flask WebSocket server behavior

Given a Flask app using Flask-SocketIO, what will happen when the server emits a message to a connected client?

Flask
from flask import Flask
from flask_socketio import SocketIO, emit

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

@socketio.on('connect')
def handle_connect():
    emit('message', {'data': 'Welcome!'})
AThe client will receive the message only after refreshing the page.
BThe client must send a message first before receiving any data from the server.
CThe server will raise an error because emit cannot be called inside the connect event.
DThe client receives the 'Welcome!' message immediately after connecting without needing to request it.
Attempts:
2 left
💡 Hint

Consider what happens when a client connects to a WebSocket server.

📝 Syntax
advanced
2:00remaining
Correct Flask WebSocket event handler syntax

Which option shows the correct syntax for a Flask-SocketIO event handler that listens for a 'chat message' event and sends a response?

A
@socketio.on('chat message')
def handle_message(msg):
    emit('response', {'data': msg})
B
@socketio.on_event('chat message')
def handle_message(msg):
    emit('response', {'data': msg})
C
@socketio.event('chat message')
def handle_message(msg):
    emit('response', {'data': msg})
D
@socketio.listen('chat message')
def handle_message(msg):
    emit('response', {'data': msg})
Attempts:
2 left
💡 Hint

Check the Flask-SocketIO documentation for the correct decorator to listen for events.

🔧 Debug
advanced
2:00remaining
Debugging Flask WebSocket connection issue

A developer uses Flask-SocketIO but clients never receive messages. The server code is:

from flask import Flask
from flask_socketio import SocketIO, emit

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

@socketio.on('connect')
def on_connect():
    emit('message', {'data': 'Hello'})

if __name__ == '__main__':
    app.run()

What is the main reason clients do not receive messages?

AThe server is started with app.run() instead of socketio.run(app), so WebSocket support is not enabled.
BThe emit call inside the connect event is invalid and causes a runtime error.
CThe client must send a 'connect' event manually before receiving messages.
DThe Flask app is missing a route for '/' which prevents connections.
Attempts:
2 left
💡 Hint

Consider how Flask-SocketIO requires the server to be started.

❓ state_output
expert
3:00remaining
State behavior in Flask WebSocket with multiple clients

Consider a Flask-SocketIO server that keeps a counter of connected clients and broadcasts it on each connection and disconnection:

from flask import Flask
from flask_socketio import SocketIO, emit

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

connected_clients = 0

@socketio.on('connect')
def on_connect():
    global connected_clients
    connected_clients += 1
    socketio.emit('clients', {'count': connected_clients})

@socketio.on('disconnect')
def on_disconnect():
    global connected_clients
    connected_clients -= 1
    socketio.emit('clients', {'count': connected_clients})

If three clients connect one after another, then one disconnects, what is the sequence of 'count' values broadcasted?

A3, 2, 1, 0
B1, 2, 3, 2
C0, 1, 2, 3
D1, 1, 1, 0
Attempts:
2 left
💡 Hint

Track the value of connected_clients after each event.

Practice

(1/5)
1. What is a common alternative to Server-Sent Events (SSE) for real-time communication in Flask applications?
easy
A. Static HTML pages
B. Regular HTTP GET requests
C. WebSockets using Flask-SocketIO
D. Email notifications

Solution

  1. Step 1: Understand SSE purpose

    Server-Sent Events send updates one-way from server to browser for real-time data.
  2. Step 2: Identify alternative for two-way communication

    WebSockets allow two-way real-time communication and Flask-SocketIO simplifies their use in Flask.
  3. Final Answer:

    WebSockets using Flask-SocketIO -> Option C
  4. Quick Check:

    Real-time two-way communication = WebSockets [OK]
Hint: WebSockets enable two-way real-time updates, unlike SSE [OK]
Common Mistakes:
  • Confusing SSE with regular HTTP requests
  • Thinking static pages can update in real-time
  • Assuming email is real-time communication
2. Which of the following is the correct way to import Flask-SocketIO in a Flask app?
easy
A. from flask_socketio import SocketIO
B. import flask_socketio.SocketIO
C. from flask import SocketIO
D. import SocketIO from flask_socketio

Solution

  1. Step 1: Recall correct import syntax in Python

    Python uses 'from module import class' to import specific classes.
  2. Step 2: Match Flask-SocketIO import pattern

    The correct import is 'from flask_socketio import SocketIO' to get the SocketIO class.
  3. Final Answer:

    from flask_socketio import SocketIO -> Option A
  4. Quick Check:

    Correct Python import syntax = from flask_socketio import SocketIO [OK]
Hint: Use 'from module import class' for specific imports in Python [OK]
Common Mistakes:
  • Using JavaScript import syntax in Python
  • Importing SocketIO from flask instead of flask_socketio
  • Incorrect module or class names
3. Given this Flask-SocketIO code snippet, what will the client receive when the server emits 'message' event?
from flask_socketio import SocketIO, emit

socketio = SocketIO(app)

@socketio.on('connect')
def handle_connect():
    emit('message', {'data': 'Welcome!'})
medium
A. No message is sent on connect
B. A plain text message 'Welcome!'
C. An error because emit needs a string message
D. A JSON object with {'data': 'Welcome!'}

Solution

  1. Step 1: Understand the emit function usage

    emit sends an event with a dictionary payload as JSON to the client.
  2. Step 2: Analyze the event and data sent on connect

    On 'connect', the server emits 'message' event with {'data': 'Welcome!'} dictionary.
  3. Final Answer:

    A JSON object with {'data': 'Welcome!'} -> Option D
  4. Quick Check:

    emit sends JSON data = A JSON object with {'data': 'Welcome!'} [OK]
Hint: emit sends JSON-like dict, not plain text [OK]
Common Mistakes:
  • Thinking emit sends plain text only
  • Assuming emit requires string, not dict
  • Believing no message is sent on connect
4. Identify the error in this Flask-SocketIO server code:
from flask_socketio import SocketIO

socketio = SocketIO(app)

@socketio.on('message')
def handle_message(data):
    print('Received:', data)

socketio.run(app)
medium
A. Function handle_message missing return statement
B. Missing import of Flask app
C. No event handler for 'connect'
D. Using socketio.run instead of app.run

Solution

  1. Step 1: Check imports and app definition

    The code uses 'app' but does not import or define it anywhere.
  2. Step 2: Confirm socketio.run usage

    socketio.run(app) is correct to run the app with SocketIO support.
  3. Final Answer:

    Missing import of Flask app -> Option B
  4. Quick Check:

    Undefined app variable = Missing import of Flask app [OK]
Hint: Always define or import Flask app before using it [OK]
Common Mistakes:
  • Confusing socketio.run with app.run
  • Thinking event handlers must return values
  • Assuming 'connect' event handler is mandatory
5. You want to replace Server-Sent Events with Flask-SocketIO for a chat app. Which approach correctly handles broadcasting a message to all connected clients?
hard
A. Use socketio.emit('chat', message, broadcast=True) inside an event handler
B. Use emit('chat', message) without broadcast inside the handler
C. Use socketio.send(message) without event name
D. Use app.run() with broadcast=True parameter

Solution

  1. Step 1: Understand broadcasting in Flask-SocketIO

    To send a message to all clients, use socketio.emit with broadcast=True.
  2. Step 2: Evaluate options for broadcasting

    Use socketio.emit('chat', message, broadcast=True) inside an event handler correctly uses socketio.emit with event name and broadcast flag.
  3. Final Answer:

    Use socketio.emit('chat', message, broadcast=True) inside an event handler -> Option A
  4. Quick Check:

    Broadcast all clients = socketio.emit with broadcast=True [OK]
Hint: Broadcast with socketio.emit and broadcast=True [OK]
Common Mistakes:
  • Omitting broadcast=True to send to all clients
  • Using emit without socketio object for broadcast
  • Trying to broadcast via app.run parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard