#🔒 websocket in quart

31 messages · Page 1 of 1 (latest)

jade stone
#

I’m unsure why the websocket variable is being overwritten with a new websocket. When a message is sent from the User to the Bot, it unexpectedly gets redirected back to the User’s websocket. Here’s the relevant code:

lime narwhalBOT
#

@jade stone

Python help channel opened

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.

jade stone
jade stone
#

please help me

valid minnow
jade stone
#

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

valid minnow
#

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())```

jade stone
#

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?

valid minnow
#

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

jade stone
#

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

valid minnow
jade stone
valid minnow
#

carefully yes, you need to make sure the tasks get cancelled if an error occurs, like the connection being reset or whatever

jade stone
#

Ok thank you I will try it tmr thank you so much

sonic wedge
#

You could use quart-trio or anyio Task Groups instead while you wait for 3.11

lime narwhalBOT
#
Python help channel closed

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.