Search code examples
javascriptpythonwebsocketelectronaiohttp

electron/socket.io client - python-socketio/aiohttp server // fail to connect


This websocket connection fails. The funny thing is this was working a couple days ago. I downgraded electron from 6 to 5.0.6 but this didn't help.

enter image description here enter image description here

Server

from aiohttp import web
import socketio


app = web.Application()
sio = socketio.AsyncServer()
# or sio = socketio.AsyncServer(cors_allowed_origins='*')
sio.attach(app)

@sio.on('connect')
async def user_connected(sid, arg):
    print(sid)
    print(arg)

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

Client (also tested from pure node)

const con = 'http://0.0.0.0:8080';
const socket = require('socket.io-client').connect(con);

socket.on('connect', (e) => console.log(e));

When I try it in the browser, I get cors error. Thus, I allowed cors which made it work

sio = socketio.AsyncServer(cors_allowed_origins='*')

When I run this from electron/node now, I get the same error as initially plus a key error: enter image description here

Since it was working before, and now it doesn't on 2 different machines and with a minimal example project, I have no idea what could cause all of this.

Please help me stack people.


Solution

  • 2021: Note that you can now install again from pypi. You still need to allow cors.

    After a chat with Miguel Grinberg on twitter, I found out how to set it up. There are a couple of things. First 4.3 introduced CORS restriction by default. There was a bug with CORS headers from node client, since node is not a browser. That is fixed by now.

    However, it is still not enough to simply pip install python-socketio You have to install the master branch of python-enginio manually as there is the CORS header fix.

    My Pipfile looks like this:

    [packages]
    aiohttp = "*"
    aiohttp-cors = "*"
    python-enginio = {git = "https://github.com/miguelgrinberg/python-engineio/",ref = "master"}
    python-socketio = "*"
    

    Dont forget to allow CORS now serverside.

    sio = socketio.AsyncServer(cors_allowed_origins='*')