Python Server Sent Events


Server Sent Events (SSE) is a unidirectional connection between a server and a client (usually a web browser) that allows the server to "push" information to the client. It is much like websockets and long polling. The main difference between SSE and websockets is that SSE is unidirectional, only the server can send info to the client, where as with websockets, both can send info to eachother. SSE is typically considered to be much simpler to use/implement than websockets.

Asyncio SSE

This example uses the asyncio SSE library:

import asyncio
import sse

class Handler(sse.Handler):
    def handle_request(self):
        yield from asyncio.sleep(2)
        yield from asyncio.sleep(2)
        self.send('bar', event='wakeup')

start_server = sse.serve(Handler, 'localhost', 8888)

Flask SSE

def stream():
    def event_stream():
        while True:
            if message_to_send:
                yield "data: 
    return Response(event_stream(), mimetype="text/event-stream")