INTEGRATIONS FastStream brokers are very easy to integrate with any of your applications: it is enough to initialize the broker at startup and close it correctly at the end of your application.
Most HTTP frameworks have built-in lifecycle hooks for this.
FastAPI Litestar Aiohttp Blacksheep Falcon Quart Sanic
Tip
If you want to use FastStream in conjunction with FastAPI , perhaps you should use a special plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from contextlib import asynccontextmanager
from fastapi import FastAPI
from faststream.kafka import KafkaBroker
broker = KafkaBroker ( "localhost:9092" )
@broker . subscriber ( "test" )
async def base_handler ( body ):
print ( body )
@asynccontextmanager
async def lifespan ( app : FastAPI ):
await broker . start ()
yield
await broker . stop ()
app = FastAPI ( lifespan = lifespan )
@app . get ( "/" )
def read_root ():
return { "Hello" : "World" }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 from litestar import Litestar , get
from faststream.rabbit import RabbitBroker
broker = RabbitBroker ( "amqp://guest:guest@localhost:5672/" )
@broker . subscriber ( "queue" )
async def handle ( msg ):
print ( msg )
@get ( "/" )
async def index () -> str :
return "Hello, world!"
app = Litestar (
[ index ],
on_startup = [ broker . start ],
on_shutdown = [ broker . stop ],
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 from aiohttp import web
from faststream.kafka import KafkaBroker
broker = KafkaBroker ( "localhost:9092" )
@broker . subscriber ( "test" )
async def base_handler ( body ):
print ( body )
async def start_broker ( app ):
await broker . start ()
async def stop_broker ( app ):
await broker . stop ()
async def hello ( request ):
return web . Response ( text = "Hello, world" )
app = web . Application ()
app . add_routes ([ web . get ( "/" , hello )])
app . on_startup . append ( start_broker )
app . on_cleanup . append ( stop_broker )
if __name__ == "__main__" :
web . run_app ( app )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 from blacksheep import Application
from faststream.kafka import KafkaBroker
broker = KafkaBroker ( "localhost:9092" )
app = Application ()
@broker . subscriber ( "test" )
async def base_handler ( body ):
print ( body )
@app . on_start
async def start_broker ( application : Application ) -> None :
await broker . start ()
@app . on_stop
async def stop_broker ( application : Application ) -> None :
await broker . stop ()
@app . route ( "/" )
async def home ():
return "Hello, World!"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 import falcon
import falcon.asgi
from faststream.kafka import KafkaBroker
broker = KafkaBroker ( "localhost:9092" )
@broker . subscriber ( "test" )
async def base_handler ( body ):
print ( body )
class ThingsResource :
async def on_get ( self , req , resp ):
resp . status = falcon . HTTP_200
resp . content_type = falcon . MEDIA_TEXT
resp . text = (
" \n Two things awe me most, the starry sky "
"above me and the moral law within me. \n "
" \n "
" ~ Immanuel Kant \n\n "
)
class StreamMiddleware :
async def process_startup ( self , scope , event ):
await broker . start ()
async def process_shutdown ( self , scope , event ):
await broker . stop ()
app = falcon . asgi . App ()
app . add_middleware ( StreamMiddleware ())
app . add_route ( "/things" , ThingsResource ())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 from quart import Quart
from faststream.kafka import KafkaBroker
broker = KafkaBroker ( "localhost:9092" )
app = Quart ( __name__ )
@broker . subscriber ( "test" )
async def base_handler ( body ):
print ( body )
@app . before_serving
async def start_broker ():
await broker . start ()
@app . after_serving
async def stop_broker ():
await broker . stop ()
@app . route ( "/" )
async def json ():
return { "hello" : "world" }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 from sanic import Sanic
from sanic.response import text
from faststream.kafka import KafkaBroker
broker = KafkaBroker ( "localhost:9092" )
app = Sanic ( "MyHelloWorldApp" )
@broker . subscriber ( "test" )
async def base_handler ( body ):
print ( body )
@app . after_server_start
async def start_broker ( app , loop ):
await broker . start ()
@app . after_server_stop
async def stop_broker ( app , loop ):
await broker . stop ()
@app . get ( "/" )
async def hello_world ( request ):
return text ( "Hello, world." )
However, even if such a hook is not provided, you can do it yourself.
And not only HTTP frameworks.