#🔒 websocket in quart
31 messages · Page 1 of 1 (latest)
@jade stone
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
main.py and object.lpy
https://paste.pythondiscord.com/BQYA
please help me
i dont really understand the code but im wondering, is USERS.get(data.get("user_id")) correct? your login_required decorator uses the bearer token as the key, but isn't that different from their user ID?
oh wait you are correct i did something wrong here
sorry let me have a quick fix
but the problem i got was the message didn't sent to bot instead it sent to the user
but if i sent the message using await websocket.send_json() in ws_bot(main.py) it will sent to the bot
https://quart.palletsprojects.com/en/latest/how_to_guides/websockets.html
https://quart.palletsprojects.com/en/latest/tutorials/chat_tutorial.html#building-a-broker
websocket seems to be a contextual variable in that it always refers to one object within the same request
so even though it looks like you have different connection objects stored in _websocket, within one task/request they all refer to the same websocket connection
you'll probably need to follow the broker design in the second link, where each request creates a queue for other tasks to submit messages to it
perhaps something along the lines of: ```py
class User:
def init(self):
self._queue = asyncio.Queue()
def send_nowait(self, message):
# Call this from Bot and vice versa
self._queue.put_nowait(message)
async def send_loop(self):
while True:
message = await self._queue.get()
await websocket.send(message)
@app.websocket("/ws_user")
@login_required
async def ws_user(user: User) -> None:
async with asyncio.TaskGroup() as tg:
tg.create_task(user.recv_loop())
tg.create_task(user.send_loop())```
oh i see, it is the first time im using contextual variable
let me try it
do i have to do it in the bot object too?
ya, if you directly call Bot.send() from User, the context of ws_user will make it refer to the user's connection instead
the last time i tried websockets was with the websockets library where each connection was an actual object you could store, so its a bit weird looking at quart handling it with context vars
yea, but thank you so much
i will try it now
ooo sorry one question
How do I handle disconnection?
and i don't have TaskGroup in asyncio
oh i need at least python 3.11
its convenient for waiting on and cancelling tasks in sync, but i guess you can make do with a try/finally + asyncio.gather() + task.cancel()
You mean I can use asyncio.gather instead of TaskGroup?
carefully yes, you need to make sure the tasks get cancelled if an error occurs, like the connection being reset or whatever
Ok thank you I will try it tmr thank you so much
You could use quart-trio or anyio Task Groups instead while you wait for 3.11
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.