0
0
Flaskframework~10 mins

Server-Sent Events alternative in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Server-Sent Events alternative
Client sends HTTP request
Server holds connection open
Server sends data chunks
Client receives and processes data
Connection closes or repeats
The server keeps the connection open and sends data chunks to the client as events happen, without the client needing to request repeatedly.
Execution Sample
Flask
from flask import Flask, Response
import time

app = Flask(__name__)

@app.route('/stream')
def stream():
    def event_stream():
        for i in range(3):
            yield f'data: Message {i}\n\n'
            time.sleep(1)
    return Response(event_stream(), mimetype='text/event-stream')
This Flask code streams three messages to the client, one every second, using Server-Sent Events.
Execution Table
StepActionData SentSleep Time (s)Connection State
1Start event_stream generatorNone0Open
2Yield 'data: Message 0\n\n'Message 01Open
3Yield 'data: Message 1\n\n'Message 11Open
4Yield 'data: Message 2\n\n'Message 21Open
5Generator endsNone0Closed
💡 Generator finishes after sending 3 messages, connection closes.
Variable Tracker
VariableStartAfter 1After 2After 3Final
iNone012End
Connection StateOpenOpenOpenOpenClosed
Key Moments - 2 Insights
Why does the connection stay open after sending each message?
Because the generator yields data but does not end until all messages are sent, keeping the HTTP connection open as shown in steps 2-4 of the execution_table.
What causes the connection to close?
When the generator finishes yielding all messages (step 5), Flask closes the connection automatically.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'i' when the second message is sent?
A0
B1
C2
DNone
💡 Hint
Check the 'Data Sent' and 'Step' columns for when the second message is sent.
At which step does the connection close according to the execution_table?
AStep 3
BStep 4
CStep 5
DStep 2
💡 Hint
Look at the 'Connection State' column for when it changes to 'Closed'.
If we increase the range to 5 in the generator, how would the 'After 3' value of 'i' in variable_tracker change?
AIt would be 2
BIt would be 3
CIt would be 4
DIt would be 5
💡 Hint
The 'After 3' column shows the value after the third iteration, which is index 2 starting from 0.
Concept Snapshot
Server-Sent Events (SSE) let a server push updates to a client over HTTP.
In Flask, use a generator function yielding 'data:' lines.
Return Response with mimetype 'text/event-stream'.
Connection stays open until generator ends.
Client receives messages as they arrive without polling.
Full Transcript
Server-Sent Events (SSE) provide a way for a server to send real-time updates to a client by keeping an HTTP connection open and streaming data. In Flask, this is done by creating a generator function that yields messages prefixed with 'data:' and separated by double newlines. The Flask route returns a Response object with the mimetype set to 'text/event-stream'. The connection remains open while the generator yields messages, and closes when the generator finishes. This allows clients to receive updates without repeatedly asking the server, making it efficient for live data feeds.