python-trio / trio-websocket

WebSocket client and server implementation for Python Trio
MIT License
70 stars 26 forks source link

How to serve with Hypercorn #176

Closed realsama closed 1 year ago

realsama commented 1 year ago

Hello there,

How can I serve my server implementation with Hypercorn? I am getting an "Address already in use" because Hypercorn is bound to the port.

My main looks like below.

async def main(*args, **kwargs):
    async with trio.open_nursery() as nursery:
        nursery.start_soon(listen_redis)

        await serve_websocket(
            echo_server,
            "127.0.0.1",
            5400,
            ssl_context=None,
            handler_nursery=nursery,
        )

All help will be appreciated. Thanks!

belm0 commented 1 year ago

There was an old ticket #1 asking about how to upgrade an HTTP connection to websocket, but I don't know how to do it.

If you only need a websocket server, I suggest looking at quart-trio, which is built on hypercorn.

If you happen to only need a server, using Quart via the quart-trio extension may suffice. While trio-websocket is more flexible, Quart covers both HTTP and WebSocket within a single framework, and serving both from the same port is straightforward. There has yet to be a performance comparison.

realsama commented 1 year ago

There was an old ticket #1 asking about how to upgrade an HTTP connection to websocket, but I don't know how to do it.

If you only need a websocket server, I suggest looking at quart-trio, which is built on hypercorn.

If you happen to only need a server, using Quart via the quart-trio extension may suffice. While trio-websocket is more flexible, Quart covers both HTTP and WebSocket within a single framework, and serving both from the same port is straightforward. There has yet to be a performance comparison.

I am more interested in a pure websocket only library sadly. Looks like I will just maintain my starlette implementation for now. Thanks.