-
So I realize this project is intended for RPC between servers, wrapping fastapi_websocket_rpc. I do see some hints at browser
And it seems like this project could really streamline FastAPI WebSocket endpoints even just for that use-case (browser/mobile clients). In particular, the auto-handling of Anyway, that said I'm not clear on how to have the server both subscribe & publish. Eg, what I'm looking for in pseudo-code would be:
That is, it's using both subscribe and publish. On the README there's separate setups for pub vs sub (ie: PubSubEndpoint vs PubSubClient). It strikes me they can both be used together on the FastAPI server, but I'm not sure how. The PubSubClient takes a server_uri, which in this scenario should just be "self" (or, PubSubClient should be available directly from PubSubEndpoint?). And the client_example loops TL;DR: how, just on the FastAPI server, can I both subscribe to messages from the browser, and publish things back? BTW, it may sound like I'm fitting circle to square peg, but I actually want to use this project anyway for server->server RPC (separate); so if it can streamline both scenarios, this would be a very valuable project indeed. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hey @lefnire - I'm super happy you see the value in this package. To the specific question: "how, just on the FastAPI server, can I both subscribe to messages from the browser, and publish things back?"You're pseudo-code is not far off, just need to add a callback ;-) You have two options to add subscribing logic at the backend:
app = FastAPI()
# PubSub websocket endpoint
endpoint = PubSubEndpoint()
endpoint.register_route(app, "/pubsub")
# receive an event and publish another (this time for the client)
async def event_callback(subscription:Subscription, data):
logger.info(f"Got topic {subscription.topic} - re-publishing as {CLIENT_TOPIC}")
asyncio.create_task(endpoint.publish([CLIENT_TOPIC], data))
@app.on_event("startup")
async def startup():
# subscribe to our own events
await endpoint.subscribe([SERVER_TOPIC], event_callback)
# Regular REST endpoint - that publishes to PubSub
setup_server_rest_route(app, endpoint)
uvicorn.run(app, port=PORT)
Hope this helps, feel free to follow up with more questions. |
Beta Was this translation helpful? Give feedback.
-
This is fantastic, thanks so much for the very thorough follow-up! Very cool project |
Beta Was this translation helpful? Give feedback.
-
I'm curious what folks are using for a frontend pubsub model using FastAPI. Something like actioncable for FastAPI. |
Beta Was this translation helpful? Give feedback.
Hey @lefnire - I'm super happy you see the value in this package.
And yes this can definitely work well for mobile, web, and even edge / IoT- the network layout and protocol are designed for scale out cases. - We're even thinking of creating JS clients in the future (though no immediate plans yet).
To the specific question:
"how, just on the FastAPI server, can I both subscribe to messages from the browser, and publish things back?"
You're pseudo-code is not far off, just need to add a callback ;-)
You have two options to add subscribing logic at the backend:
endpoint.notifier.subscribe(id, topics, callback)
but I just…