Server-Sent Events (SSE)

After learning about form data handling, Batman realized he needed a way to push real-time updates to his crime monitoring dashboard. Criminals don't wait for Batman to refresh his browser!

He discovered Server-Sent Events (SSE) - a perfect solution for one-way communication from server to client over HTTP. SSE allows Batman to stream live data to his dashboard without the complexity of full bidirectional communication.

"This is exactly what I need for my crime alerts!" Batman exclaimed. "I can push updates to the dashboard instantly when new crimes are detected."

Server-Sent Events are ideal for:

  • Real-time notifications
  • Live data feeds
  • Progress updates
  • Chat applications (server-to-client only)
  • Dashboard updates
  • Log streaming

How does it work?

Batman can create Server-Sent Events streams by using the SSE_Response and SSE_Message classes:

SSE Response

GET
/events
from robyn import Robyn, SSE_Response, SSE_Message
import time

app = Robyn(__file__)

@app.get("/events")
def stream_events(request):
    def event_generator():
        for i in range(10):
            yield SSE_Message(f"Event {i}", id=str(i))
            time.sleep(1)
    
    return SSE_Response(event_generator())

What's next?

Batman has mastered Server-Sent Events and can now stream real-time updates to his crime dashboard. While SSE is perfect for one-way communication from server to client, Batman realizes he needs bidirectional communication for more interactive features like real-time chat with his allies.

Next, he wants to explore how to handle bidirectional communication with WebSockets for more interactive features.

If Batman needs to handle unexpected situations, he'll learn about Exception handling to make his applications more robust.

For scaling his crime monitoring system across multiple processes, Batman will explore Scaling the Application.