Trouble with using python-engineio, python-socketio and Flask-SocketIO in my Python flask application

27 Views Asked by At

I am currently developing a flask application with Python and have a few different requirements.

First off, I need to implement a websocket server in order to have real time communication with clients (pushing various events / notifications). I managed to achieve this by installing the following package versions:

Flask-SocketIO==5.3.6
python-engineio==4.9.0
python-socketio==5.11.1

and in my client, which is an html file, I have the following to import socket.io:

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>

After initializing my Flask app, I create this flask socketio server like so:

socket = SocketIO(app, async_mode=None, logger=False, engineio_logger=False)

That's all well and good. Clients are able to connect and receive emitted events from the server.

But now is where things start to get tricky for me. In addition to this websocket with connected clients, I need to create another websocket to a different server in order to listen to emitted events, as these will be then communicated to clients in some way.

In order to do this I do the following:

import socketio
sio = socketio.Client()
sio.connect(os.getenv("HOST"))
sio.emit("authenticate", {"token":os.getenv("KEY")})

Then I have a couple of functions that are listening with decorators like @sio.on("event").

When I start up my app using gunicorn (environment/bin/gunicorn -k eventlet -w 1 -b 127.0.0.1:5000 app:app), I get the following terminal output:

[2024-03-22 15:00:22 +0100] [63715] [INFO] Starting gunicorn 21.2.0
[2024-03-22 15:00:22 +0100] [63715] [INFO] Listening at: http://127.0.0.1:5000 (63715)
[2024-03-22 15:00:22 +0100] [63715] [INFO] Using worker: eventlet
[2024-03-22 15:00:22 +0100] [63716] [INFO] Booting worker with pid: 63716
[2024-03-22 15:00:23 +0100] [63716] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/Users/charlesdupont/Documents/repos/trading-game/environment/lib/python3.12/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
    worker.init_process()
  File "/Users/charlesdupont/Documents/repos/trading-game/environment/lib/python3.12/site-packages/gunicorn/workers/geventlet.py", line 143, in init_process
    super().init_process()
  File "/Users/charlesdupont/Documents/repos/trading-game/environment/lib/python3.12/site-packages/gunicorn/workers/base.py", line 134, in init_process
    self.load_wsgi()
  File "/Users/charlesdupont/Documents/repos/trading-game/environment/lib/python3.12/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
    self.wsgi = self.app.wsgi()
                ^^^^^^^^^^^^^^^
  File "/Users/charlesdupont/Documents/repos/trading-game/environment/lib/python3.12/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
                    ^^^^^^^^^^^
  File "/Users/charlesdupont/Documents/repos/trading-game/environment/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/charlesdupont/Documents/repos/trading-game/environment/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/charlesdupont/Documents/repos/trading-game/environment/lib/python3.12/site-packages/gunicorn/util.py", line 371, in import_app
    mod = importlib.import_module(module)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/Users/charlesdupont/Documents/repos/trading-game/app.py", line 51, in <module>
    sio.connect(os.getenv("PORT"))
  File "/Users/charlesdupont/Documents/repos/trading-game/environment/lib/python3.12/site-packages/socketio/client.py", line 154, in connect
    raise exceptions.ConnectionError(exc.args[0]) from None
socketio.exceptions.ConnectionError: OPEN packet not returned by server
[2024-03-22 15:00:23 +0100] [63716] [INFO] Worker exiting (pid: 63716)
[2024-03-22 15:00:23 +0100] [63715] [ERROR] Worker (pid:63716) exited with code 3
[2024-03-22 15:00:23 +0100] [63715] [ERROR] Shutting down: Master
[2024-03-22 15:00:23 +0100] [63715] [ERROR] Reason: Worker failed to boot.

I'm not sure where the issue lies. I'm pretty sure my package versions are compatible, but somehow this is failing. If I downgrade the python-engineio, python-socketio versions and don't install flask-socketio in a different environment, this sio.connect() works fine. But this means I am not creating my websocket server.

0

There are 0 best solutions below