0% found this document useful (0 votes)
63 views

Server-Sent Events in FastAPI using Redis Pub_Sub _ Deepdesk

Uploaded by

WonderZaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Server-Sent Events in FastAPI using Redis Pub_Sub _ Deepdesk

Uploaded by

WonderZaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk

Server-Sent Events in FastAPI using Redis


Pub/Sub
Lukas Batteau · Follow
Published in Deepdesk
2 min read · Jan 18, 2021

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)

stream.py hosted with ❤ by GitHub view raw

Open in app Sign up Sign in


It takes a generator, in this case a method, called subscribe and will pass everything
the generator yields to the client down the line.
Search

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.

What if we create a generator that is, in fact, a Redis Pub/Sub subscription?

1 async def subscribe(channel: str, redis: Redis) -> AsyncGenerator:


2 (subscription,) = await redis.subscribe(channel=Channel(channel, False))
3 while await subscription.wait_message():
4 yield {"event": "message", "data": await subscription.get()}

subscribe.py hosted with ❤ by GitHub view raw

Now, all we have to do to send updates to one or more subscribers is to publish a


message to the right channel.

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 ""

publish.py hosted with ❤ by GitHub view raw

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

messages to the console.

Make sure to install the correct dependencies:

pip install fastapi


pip install fastapi-plugins
pip install sse-starlette
pip install uvicorn

Then run the application, assuming you have a Redis instance running on the
default port:

uvicorn sse-fastapi-redis:app --reload

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

1 from aioredis import Channel, Redis


2 from fastapi import FastAPI
3 from fastapi.params import Depends
4 from fastapi_plugins import depends_redis, redis_plugin
5 from sse_starlette.sse import EventSourceResponse
6 from starlette.responses import HTMLResponse
7
8 html = """
9 <!DOCTYPE html>
10 <html>
11 <head>
12 <title>SSE</title>
13 </head>
14 <body>
15 <script>
16 const evtSource = new EventSource("http://localhost:8000/sse/stream");
17 evtSource.addEventListener("message", function(event) {
18 // Logic to handle status updates
19 console.log(event.data)
20 });
21 </script>
22 </body>
23 </html>
24 """
25
26
27 app = FastAPI()
28
29
30 @app.on_event("startup")
31 async def on_startup() -> None:
32 await redis_plugin.init_app(app)
33 await redis_plugin.init()
34
35
36 @app.on_event("shutdown")
37 async def on_shutdown() -> None:
38 await redis_plugin.terminate()
39
40
41 @app.get("/sse/demo")
42 async def get():
43 return HTMLResponse(html)
44
45
46 @app.get("/sse/publish")
47 async def get(channel: str = "default", redis: Redis = Depends(depends_redis)):
48 await redis publish(channel=channel message="Hello world!")
https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 4/12
12/9/24, 23:41 Server-Sent Events in FastAPI using Redis Pub/Sub | Deepdesk
48 await redis.publish(channel channel, message Hello world! )
49 return ""
50
51
52 @app.get("/sse/stream")
53 async def stream(channel: str = "default", redis: Redis = Depends(depends_redis)):
54 return EventSourceResponse(subscribe(channel, redis))
55
56
57 async def subscribe(channel: str, redis: Redis):
58 (channel_subscription,) = await redis.subscribe(channel=Channel(channel, False))
59 while await channel_subscription.wait_message():
60 yield {"event": "message", "data": await channel_subscription.get()}

sse fastapi redis py hosted with ❤ by GitHub view raw

Big thanks to Sairam Krish for writing https://sairamkrish.medium.com/handling-


server-send-events-with-python-fastapi-e578f3929af1, on which this article is based.

Fastapi Sse Redis Pub Sub

Follow

Written by Lukas Batteau


20 Followers · Editor for Deepdesk

CTO and Co-Founder at Deepdesk

More from Lukas Batteau and Deepdesk

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

Lukas Batteau in Deepdesk

Serving GPT-2 in Google Cloud Platform


A CloudOps Journey

Feb 14, 2020 54 1

Hamed Ghasemieh in Deepdesk

Building an information retrieval app using LangChain and ElasticSearch


In this short article, I will show how we used LangChain, ElasticSearch, and ChatGPT to provide
our customers with a question and answering…

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

Oct 12, 2023 7

Ronald Moesbergen in Deepdesk

Using Google Cloud Build with private GKE clusters


To isolate workloads from the dangers of the internet, deploying “private” GKE clusters is
recommended: the Kubernetes API endpoint is only…

Dec 15, 2021 73 2

Ronald Moesbergen in Deepdesk

Scaling workloads based on GPU utilization in GKE


https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 7/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…

Sep 22, 2021 41 1

See all from Lukas Batteau

See all from Deepdesk

Recommended from Medium

asierr.dev

Building a Scalable Video Processing Microservice: A NestJS, RabbitMQ,


and Python Guide
Mastering Efficient Video Encoding and Delivery for Modern Web Applications

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

Creating a Streaming AI Chatbot with Next.JS and FastAPI Using


“AsyncGenerator-like pattern
Introduction

May 31 1

Lists

Coding & Development


11 stories · 805 saves

Natural Language Processing


1690 stories · 1261 saves

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

Implementing Background Job Scheduling in FastAPI with APScheduler


Enable your background job scheduling in a FastAPI application using APScheduler.

Mar 22 251 5

Ankit Agarwal

Polling, WebSockets and Server Sent Events : System Design


In this article we will explore Polling, WebSockets, and Server-Sent Events. These are crucial for
real time or near real time…

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

Muzaffer Yılmaz in Trendyol Tech

How We Used Server-Sent Events (SSE) to Deliver Real-Time


Notifications on Our Backend
We will show how we used Server-Sent Events (SSE) and Redis for our notification system
using Typescript and Nestjs Framework.

May 17, 2023 774 6

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

Streaming Responses In FastAPI


FastAPI support streaming response type out of the box with StreamingResponse. There’s also
an implementation of server sent events from…

Jun 4 1

See more recommendations

https://medium.com/deepdesk/server-sent-events-in-fastapi-using-redis-pub-sub-eba1dbfe8031 12/12

You might also like