Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aiohttp Websocket client and HTTP server

I'm writing an application that needs to take in events from two sources. Some events will come from a Websocket client connection, and some will come from incoming HTTP requests. Is it possible with aiohttp to have both of these listeners in the same application, or do I need two separate executables?

I currently have a function that takes websocket events, and calls a callback on each event

async def callback(msg):
    print(msg)

async def websocket():
    session = aiohttp.ClientSession()
    async with session.ws_connect('http://example.org/websocket') as ws:

        async for msg in ws:
            if msg.type == aiohttp.WSMsgType.TEXT:
                await callback(msg.data)
            elif msg.type == aiohttp.WSMsgType.CLOSED:
                break
            elif msg.type == aiohttp.WSMsgType.ERROR:
                break

The problem is that this blocks forever, so I don't know how I could integrate this with the aiohttp server. If I do:

await websocket()
aiohttp.web.run_app(app)

then web.run_app is never called. Is there some way to accomplish this? It seems like the ideal case for asyncio, multiple things handling multiple events asynchronously. How do I do it?

like image 544
Lily Mara Avatar asked Jun 29 '26 01:06

Lily Mara


1 Answers

I was able to accomplish my goals by using the on_startup handler of my aiohttp app.

async def callback(msg):
    print(msg)

async def websocket(session):
    async with session.ws_connect('http://example.org/websocket') as ws:
        async for msg in ws:
            if msg.type == aiohttp.WSMsgType.TEXT:
                await callback(msg.data)
            elif msg.type == aiohttp.WSMsgType.CLOSED:
                break
            elif msg.type == aiohttp.WSMsgType.ERROR:
                break

async def init(app):
    session = aiohttp.ClientSession()
    app['websocket_task'] = app.loop.create_task(websocket(session))

app = web.Application()
app.on_startup.append(init)
like image 193
Lily Mara Avatar answered Jun 30 '26 19:06

Lily Mara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!