zella
zella

Reputation: 4685

How to mix async socket io with aiohttp

I want to write http server with socket io. What I need:

request --> socket io ask -> socket io answer -> response

On http request, I send message to socket io client and wait response message from socket io. Then send this message as http response or timeout. Here "getting started" code, which I want to adopt.

from aiohttp import web
import socketio

sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)

async def index(request):
    sio.emit('ask', 'some data')
    # here I want wait socket io answer
    return web.Response(..., content_type='text/plain')

@sio.on('connect', namespace='/chat')
def connect(sid, environ):
    print("connect ", sid)

@sio.on('answer', namespace='/chat')
async def answer(sid, data):
    # here I want send response to index or timeout
    ...

@sio.on('disconnect', namespace='/chat')
def disconnect(sid):
    print('disconnect ', sid)

app.router.add_get('/', index)

if __name__ == '__main__':
    web.run_app(app)

I dont understund how to link http part with socket io

Upvotes: 4

Views: 2812

Answers (1)

Cloudomation
Cloudomation

Reputation: 1662

You could use asyncio.Queue for that:

from aiohttp import web
import socketio
import asyncio

queue = asyncio.Queue()  # create queue object
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)

async def index(request):
    sio.emit('ask', 'some data')
    response = await queue.get()  # block until there is something in the queue
    return web.Response(response, content_type='text/plain')

@sio.on('connect', namespace='/chat')
def connect(sid, environ):
    print("connect ", sid)

@sio.on('answer', namespace='/chat')
async def answer(sid, data):
    await queue.put(data)  # push the response data to the queue

@sio.on('disconnect', namespace='/chat')
def disconnect(sid):
    print('disconnect ', sid)

app.router.add_get('/', index)

if __name__ == '__main__':
    web.run_app(app)

Note:

to handle multiple concurrent sessions you should create a separate asyncio.Queue object for each session. Otherwise clients could receive the data which was requested in a different session.

Upvotes: 2

Related Questions