Skip to content

How to use as just a Websocket server (for browser/mobile client)? #5

@lefnire

Description

@lefnire

So I realize this project is intended for RPC between servers, wrapping fastapi_websocket_rpc. I do see some hints at browser

easily accessible and scalable over the web and across your cloud in realtime

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 broadcaster while still exposing on_connect|disconnect, marshaling request/response via Pydantic, etc. Things I'm struggling with using FastAPI WebSockets "raw" + broadcaster (discussion).

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:

sockets = []

app.on_connect(websocket):
  sockets.append(websocket)

app.subscribe(topic, data):
  if topic == 'chat':
    save_to_db(data)
    for s in sockets:
      if s.hasPermission(topic):
        endpoint.publish('new-message', data)

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 main() in asyncio.run; which in the above scenario wouldn't be compatible with FastAPI's main loop (right)? Looking through the code, it strikes me that maybe I want to instantiate a custom EventNotifier and pass that to PubSubEndpoint, so I can both broadcast & listen - but I'm not sure I'm barking up the right tree.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions