Server-Sent Events in FastAPI using Redis Pub_Sub _ Deepdesk
Server-Sent Events in FastAPI using Redis Pub_Sub _ Deepdesk
Listen Share
Whenever you want real-time updates of your client, and you only need to push
changes to the client (one-way traffic), there is a great alternative to WebSockets,
called Server-Sent Events.
Server-Sent Events (SSE) basically means the HTTP response of a GET request is a
never-ending stream, and you get to push changes down this stream to the client.
In this article, I would like to show how you can set up SSE in FastAPI using Redis
Pub/Sub.
There is a neat little SSE library called sse-starlette, that already provides a Server-
Sent Events response type, like so:
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 1/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
1 @app.get("/sse/stream")
2 async def stream():
3 return EventSourceResponse(subscribe)
The question is, to which client? In some use cases, it’s OK to push changes to all
listeners, but sometimes you want to specify a topic or a channel. You can even use
this to limit the receiver to one particular user.
Enter Redis Pub/Sub. With Redis Pub/Sub you publish to channels and have
subscribers receive these updates. You can probably see where this is going.
1 @app.get("/sse/publish")
2 async def get(channel: str = "default", redis: Redis = Depends(depends_redis)):
3 await redis.publish(channel=channel, message="Hello world!")
4 return ""
As you can see we are using a dependency here for our Redis connection
(dependency injection being one of the great improvements of FastAPI). It is
actually a third-party dependency, called fastapi-plugins. It’s great because it is
based on aioredis, a fully async Redis client.
Here is the full source code that combines the above examples into a working
FastAPI application, including a demo JavaScript listener that prints incoming
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 2/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
Then run the application, assuming you have a Redis instance running on the
default port:
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 3/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
Follow
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 5/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 6/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
With GPU accelerated workloads becoming more common, scaling workloads based on CPU is
no longer optimal. Instead, it would be better to…
asierr.dev
Mar 15 1
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 8/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
Chao Cheng
May 31 1
Lists
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 9/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
Rajan Sahu
Mar 22 251 5
Ankit Agarwal
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 10/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
Jun 10 3
Ab Hassanein
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 11/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
Jun 4 1
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 12/12