Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Should WebSocketConnection be an asynchronous iterator? #29

Open
mehaase opened this issue Sep 17, 2018 · 4 comments
Open

Should WebSocketConnection be an asynchronous iterator? #29

mehaase opened this issue Sep 17, 2018 · 4 comments
Labels
enhancement New feature or request

Comments

@mehaase
Copy link
Contributor

mehaase commented Sep 17, 2018

The connection object could be an iterator that yields incoming messages, i.e. as an alternative to calling get_message().

# simple WebSocket echo
async with open_websocket(…) as websocket:
    async for message in websocket:
        await websocket.send(message)

Thoughts?

@mehaase
Copy link
Contributor Author

mehaase commented Oct 18, 2018

Note: Aaugustin's Websockets has async iterable connection objects: https://websockets.readthedocs.io/en/stable/api.html#module-websockets.protocol

On Python ≥ 3.6, WebSocketCommonProtocol instances support asynchronous iteration:

async for message in websocket:
   await process(message)

The iterator yields incoming messages. It exits normally when the connection is closed with the status >code 1000 (OK) or 1001 (going away). It raises a ConnectionClosed exception when the connection is >closed with any other status code.

@belm0
Copy link
Member

belm0 commented Oct 18, 2018

Raising only on connection error sounds good

@mehaase mehaase added the enhancement New feature or request label Oct 25, 2018
@mehaase mehaase mentioned this issue Oct 26, 2018
@mehaase mehaase self-assigned this Oct 29, 2018
@mehaase mehaase removed their assignment Nov 7, 2018
@mehaase mehaase added this to the 1.0 milestone Nov 17, 2018
@mehaase mehaase assigned mehaase and unassigned mehaase May 10, 2019
@mehaase mehaase removed this from the 1.0 milestone May 17, 2019
@Ivoz
Copy link

Ivoz commented Oct 17, 2021

Yes I think it's nice idea, websockets impl is here, very simple ?

https://github.com/aaugustin/websockets/blob/13eff12bb4c995b50154fdc250281c92ddccaca0/src/websockets/legacy/protocol.py#L469

Also it seems aaugustin is moving his implementation to have a sans-io base, just for FYI

@mehaase
Copy link
Contributor Author

mehaase commented Oct 17, 2021

Oh cool, I like aaugustin’s library, nice to hear that it is adopting sans io. This library here is not maintained anymore (I’ve changed jobs twice since I wrote it) so it would be great to have an async library with an active maintainer.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants