You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Request: more documentation (having trouble grokking) #34
Sorry for the newbishness, I'm having trouble understanding how to use the project. Main points I struggle with:
Coming from FastAPI/WebSockets, the docs have Depends(...) injected to routes. They use fastapi.app.websocket rather than starlette.routing.WebSocketRoute. Wondering what the simple translation from Starlette->FastAPI is to allow Depends() in the functions?
If it's as simple as replacing WebSocketRoute("/", ...) with app.websocket("/"), where do I put my dependencies? Is it at the async def chatroom_ws(websocket, <add deps here>) level?
Where/how do I handle on_connect, on_disconnect?
What's going on with run_until_first_complete? I can't find documentation in Starlette, and when I dive into asyncio.wait(..., FIRST_COMPLETED) it seems to imply the first task to complete is solid, and what's left is cancelled? Which confuses me on the setup of receiver/sender, don't we want both?
Looking at all the above, fastapi-websocket-broadcast has a pretty solid kitchen-sink example of subclassing WebSocketEndpoint with on_connect|disconnect|message, keeping websockets around for later use, etc. Maybe a more robust broadcaster sample in /examples could be in order for us FastAPI newbies?
None of questions you've asked apply to the Broadcaster itself ;)
Broadcaster is small library that provides unified API over the top of few technologies that support publish-subscribe model. This library only does three things:
creates instance of pub-sub server via Broadcast(connection_string)
enables applications to push messages to the server with broadcast.publish(...)
enables applications to subscribe to the server to receive messages with broadcast.subscribe()
Everything else has to be implemented by the developer and is outside of this library's scope. If you take example's code and remove lines of code non-specific to the broadcaster, you will get this:
# Creates pub-sub service using Redisbroadcast=Broadcast("redis://localhost:6379")
# Sends "message" to the "chatroom" channelawaitbroadcast.publish(channel="chatroom", message=message)
# Receives messages sent to the "chatroom" channel and does something with themasyncwithbroadcast.subscribe(channel="chatroom") assubscriber:
asyncformessageinsubscriber:
do_something_with_message(message)
I'm sorry I'm unable to provide the example for FastAPI at this time, but hopefully this will make it little more clear what this library is and what it's not. :)
Right, right - definitely not directly related, so it's mostly an out-of-scope question. And thanks for the quick response! Maybe I'll just follow-up here in case someone else has the same question, as the FastAPI Websockets docs points here, and it's not clear how to fit broadcaster's pub/sub into a WebsocketEndpoint class with on_receive.
I'll take a stab at using this on a project, then post a sample implementation here if I get it working.
Activity
rafalp commentedon Mar 10, 2021
Hello!
None of questions you've asked apply to the Broadcaster itself ;)
Broadcaster is small library that provides unified API over the top of few technologies that support publish-subscribe model. This library only does three things:
Broadcast(connection_string)broadcast.publish(...)broadcast.subscribe()Everything else has to be implemented by the developer and is outside of this library's scope. If you take example's code and remove lines of code non-specific to the broadcaster, you will get this:
I'm sorry I'm unable to provide the example for FastAPI at this time, but hopefully this will make it little more clear what this library is and what it's not. :)
lefnire commentedon Mar 10, 2021
Right, right - definitely not directly related, so it's mostly an out-of-scope question. And thanks for the quick response! Maybe I'll just follow-up here in case someone else has the same question, as the FastAPI Websockets docs points here, and it's not clear how to fit
broadcaster's pub/sub into a WebsocketEndpoint class with on_receive.I'll take a stab at using this on a project, then post a sample implementation here if I get it working.