Socket.io can't connect to Sanic: Error: timeout

Hi,

I have a JS program that runs Socket.io. It works properly with Flask-SocketIO and I’m trying to migrate it to Sanic.

So, basically, I’ve added a @app.websocket(’/socket.io/’), like this:

@app.websocket("/socket.io")
async def feed(request, ws):
    while True:
        data = await ws.recv()
        print("Received: " + data)

The idea is to first validate that it works.

If I add a send data first, I have a “server error” displayed on the JS console and the connection fails.

How can I connect the two?

Ok I found out why! It’s because Socket.io uses a custom protocol and Sanic is not using it.

I need to implement python-socketio — python-socketio documentation in Sanic to make it work.

Happy to know if there are any existing library for this. Otherwise, I’ll implement one and share it.

Reading more about Socket.io recommends doing the following for Sanic:


sio = socketio.AsyncServer(async_mode='sanic')
app = Sanic()
sio.attach(app)

then, handling sio such as @sio.on('connect')...

But Sanic offers @app.websocket('/path')

What’s the best action here? Do I need to setup SocketIo in Sanic’s Websocket, or the opposite way?

@cx42net I am sorry I missed this. I saw you had also posted on SO about this as well. Is this still relevant a month later?

It’s all good now, the question on StackOverflow was answered by the author of Python-SocketIO, and before having his answer, I was reading his code and noticed that he’s simply adding a Websocket route to the Sanic App, so it’s all handled properly :slight_smile:

We can close this topic (though I don’t know how)