Replies: 1 comment 9 replies
-
Well you've already to started to recreate some of the Channels logic in your app. @websocket("/ws")
async def handler(socket: WebSocket, channels: ChannelsPlugin) -> None:
await socket.accept()
channels_to_subscribe = ["public"]
if is_authenticated(socket):
channels_to_subscribe.append("privileged")
async with channels.subscribe(channels_to_subscribe) as subscriber:
... |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I've been playing around with litestar and would like to add a websocket handler to my app with which I can send messages to authenticated and authorized clients. Basically:
I already tested the
Channels
plugin which fulfills the first two cases (at least tested for JWT, struggling to get it to work with cookies). Here, I just create a thread to periodically publish notifications. But I can't really figure out how to do message filtering based on user role for example.So, I tried to do an implementation based on
WebsocketListener
that is attached below. However, how would I access the instance of this handler so that I can call thenotify()
method? Be it for a constant loop or, for example, to implement chat room behavior (here, channels is probably better, but just for the sake of understanding the solution)?EDIT: In the meantime, managed to get this listener to work with Sessions and attached the app code below. I used
httpx
for testing, so my solution might be tailored to how that library formats data but I hope it's somewhat universal. So,on_connect
andon_disconnect
are working fine (validating the session id, getting session data). But I still don't know how I could use this for pushing messages to the client.Websocket handler
app code
Beta Was this translation helpful? Give feedback.
All reactions