quart icon indicating copy to clipboard operation
quart copied to clipboard

Async for support for websockets

Open mozesa opened this issue 3 years ago • 2 comments

Hello @pgjones,

I just would like to keep up this topic.

There was a discussion in which Nathaniel J. Smith mentioned that async for support for websockets would be more pythonic.

Currently:

while True:
    result = await websocket.receive_json()

The more idiomatic:

async for result in websocket.receive_json():
    ...

It would be great that the async for loop supports both receive and receive_json methods.

Thanks for your help in advance.

Best Regards, András

mozesa avatar Jan 22 '21 21:01 mozesa

Is this something there is interest in? One couldn't convert the receive and receive_json methods to generators w/o breaking backwards compatibility I think, but maybe additional generator functions could be added? websocket.messages() and websocket.json_messages() or something? Or maybe this would over-complicate things...

GilbertYoder avatar Dec 19 '22 16:12 GilbertYoder

Looks like starlette has adopted iter_text, iter_bytes and iter_json so iter_messages and iter_json I think make the most sense. However, I need to think of how to indicate the websocket has disconnected.

pgjones avatar Jan 16 '23 21:01 pgjones