Larz › Server-Sent Events in Python
Real-time push, over plain WSGI
You don't always need WebSockets. Server-Sent Events give you live server-to-client updates with a one-line response.
pip install larz
@app.get("/events")
def events(req):
def stream():
for i in range(10):
yield ("tick", str(i)) # (event_name, data)
time.sleep(1)
return Response.sse(stream())
Streaming responses too
@app.get("/export.csv")
def export(req):
return Response.stream(rows_as_csv(), content_type="text/csv") # large download
SSE is perfect for live dashboards, progress updates, notifications and log tails — anything one-directional. It works on any WSGI server. Full bidirectional WebSockets arrive with the async core in a future release.
FAQ
Do I need WebSockets for real-time?
Not for server-to-client updates. Server-Sent Events (Response.sse) stream over plain WSGI and cover live dashboards, progress and notifications.
Does SSE work under gunicorn?
Yes — it's a standard streaming HTTP response, so any WSGI server serves it.