This project demonstrates a real-time messaging system using Server-Sent Events (SSE) implemented with Starlette framework. It shows how to create a simple real-time communication channel from server to clients.
-
Server-Sent Events (SSE)
- One-way real-time communication from server to clients
- Automatic reconnection handling
- Efficient event streaming
-
Starlette Features
- StreamingResponse for SSE
- Route handling
- Static file serving
- ASGI application structure
-
Asynchronous Programming
- Using asyncio.Queue for message distribution
- Async/await patterns
- Proper connection handling
- Real-time message broadcasting
- Multiple client support
- Clean connection handling
- Simple message input interface
pip install starlette uvicorn
fastapi_starlette_demo/
├── sse_example.py # Main application code
├── sse.html # Frontend interface
└── README.md # This file
-
Clone or download this repository
-
Install dependencies:
pip install starlette uvicorn
-
Run the server:
python sse_example.py
-
Open your browser and visit:
http://localhost:8000
-
To test the functionality:
- Open multiple browser windows/tabs pointing to http://localhost:8000
- Type a message in any window and click "Send"
- Observe the message appearing in all windows
-
Server Side
- Creates SSE endpoints for client connections
- Maintains a set of connected clients using asyncio.Queue
- Broadcasts messages to all connected clients
- Handles client disconnections gracefully
-
Client Side
- Connects to SSE endpoint using EventSource API
- Displays received messages in real-time
- Sends new messages via POST requests
async def stream_sse(self, request):
queue = asyncio.Queue()
self.clients.add(queue)
async def event_generator():
try:
while True:
message = await queue.get()
yield f"data: {message}\n\n".encode('utf-8')
except asyncio.CancelledError:
self.clients.remove(queue)
async def broadcast_message(self, message: str):
for queue in self.clients:
await queue.put(message)
-
One-Way Communication
- SSE only allows server-to-client communication
- Simpler than WebSockets for one-way scenarios
-
HTTP-Based
- Uses regular HTTP
- Better compatibility with firewalls and proxies
-
Automatic Reconnection
- Built-in reconnection handling
- More resilient to connection drops
This pattern is particularly useful for:
- Real-time notifications
- Live feed updates
- Status updates
- Event broadcasting
- Any scenario requiring server-to-client push notifications
MIT License