12

When I development some socket.io service in python environment by using python-socketio and gunicorn, I meet an issue here.

I am using Mac OS X and I am using python 3.7.

Environment setting
$ pip install python-socketio
$ pip install gunicorn

server-side code
app.py

import socketio

sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files = {
    '/': './socketio-client.html'
})

@sio.event
def connect(sid, environ):
    print(sid, 'connected')

@sio.event
def disconnect(sid):
    print(sid, 'disconnected')

client-side code
socketio-client.html

<html>
  <head>
    <title>Socket.IO Demo</title>
    <script src="https://cdn.socket.io/3.1.3/socket.io.min.js" integrity="sha384-cPwlPLvBTa3sKAgddT6krw0cJat7egBga3DJepJyrLl4Q9/5WLra3rrnMcyTyOnh" crossorigin="anonymous"></script>
  </head>
  <body>
    <h1>Socket.IO Demo</h1>
    <script>
      const sio = io();
      sio.on('connect', () => {
        console.log('connected');
      });

      sio.on('disconnect', () => {
        console.log('disconnected');
      });
    </script>
  </body>
</html>

Start program
I put the files in the same folder. And start it by following the command.
$ gunicorn --thread 50 app:app
enter image description here

I use the browser to open http://localhost:8000 and that works. Issue But there is an issue with the server

The WebSocket transport is not available, you must install a WebSocket server that is compatible with your async mode to enable it. See the documentation for details. (further occurrences of this error will be logged with level INFO)

I try to do
$ gunicorn --log-level INFO --thread 50 app:app
But I still cannot get helpful information from INFO log-level.

The code can keep going to run but the message shows me to install WebSocket server and I don't know which kind of package I need to install for this case of python-socketio or gunicorn.

What kind of package I missed to install? It's my first time to use python-socketio and gunicorn.
What should I do next?

Thanks for your help.

Milo Chen
  • 3,617
  • 4
  • 20
  • 36
  • 1
    Have a look at the deployment options here onwards - https://python-socketio.readthedocs.io/en/latest/server.html#eventlet. – Wiggy A. Sep 07 '21 at 05:07

2 Answers2

10

It just needs to install more packages here.
$ pip install gevent-websocket
$ pip install eventlet

And then
$ gunicorn --thread 50 app:app

enter image description here

Update 1:

If the server-side need to active emit to client side, it will need this environment. Because this command $ gunicorn --thread 50 app:app cannot support this situation.

The worked environment should be set by following.

$ pip install eventlet==0.30.2
$ gunicorn -k eventlet -w 1 app:app

Milo Chen
  • 3,617
  • 4
  • 20
  • 36
0

I was deploying an application using flask and gunicorn and nginx as my reverse proxy when I encountered this error. I tried the following recommended solutions:

a) Installing more packages: $ pip install gevent-websocket $ pip install eventlet

NOTE: This did not solve my problem.

b) If you don't have a gunicorn conf file, you can create one and name it gunicorn.conf.py. You can then configure your conf file as follows:

# Gunicorn conf file
bind = '0.0.0.0:8000'
# Maximum time (in seconds) a worker is allowed to process a request
timeout = 6000
# Number of worker processes to spawn
# workers = 4
# Number of threads to be used per worker
threads = 4
# Maximum number of simultaneous clients that a single worker can handle
worker_connections = 1000
# Worker class to use (in this case, using eventlet)
worker_class = 'eventlet'

Then, you would run your application as follows: gunicorn --config-file path/to/gunicorn.conf.py -k eventlet path_to_app:app

More specifically, I ran mine as follows (I had my conf file in the directory conf/ and my app in the directory web_flask

gunicorn --config-file conf/gunicorn.conf.py -k eventlet web_flask.app:app

NOTE: If you prefer to use gevent, according to flasksocket.io documentation when deploying an application, you will have to run gunicorn as follows:

gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module:app

References: https://flask-socketio.readthedocs.io/en/latest/deployment.html#gunicorn-web-server

tykoon
  • 11
  • 4