4

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.