Flask, FlaskSocketIO - RuntimeError: Cannot obtain socket from WSGI environment
Asked Answered
C

2

10

When I try to use the functionality that uses websockets in my application, I get this error in the console:

File "/Users/user/venv/lib/python3.7/site-packages/simple_websocket/ws.py", line 138, in __init__
    raise RuntimeError('Cannot obtain socket from WSGI environment.')
RuntimeError: Cannot obtain socket from WSGI environment.

I also get this error in the browser console: WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=4&transport=websocket&sid=40NYzDgGYStMR0CEAAAJ' failed:

I tried using gevent, gevent-websocket, and eventlet, but this created other issues, and I'm not sure that I need to use gevent or eventlet for this use case.

Here's the rest of the relevant code:

__ init __.py

from flask_socketio import SocketIO
...

socketio = SocketIO()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    socketio.init_app(app, cors_allowed_origins='*')

    ...

    return app

app.py

from app import create_app, socketio

app = create_app()

if __name__ == '__main__':
    socketio.run(app)

routes.py This only accepts POST requests because I send data to this route from a Celery task

from app import socketio

...

@main_bp.route('/send_message', methods=['POST'])
def send_message():
    
    ...

    socketio.emit('test_message', {'msg':'Test'}, namespace='/test')

    return 'Results Sent'

index.html

        var socket = io.connect('http://localhost:5000/test');
        socket.on('connect', function(){
            console.log('Connected to socket')

        });

        socket.on('test_message', function(message){
            console.log('Received test message');
            }
        )

Note that in the browser console I'll see "Connected to socket", but not "Received test message"

Cyclist answered 17/6, 2021 at 12:37 Comment(0)
I
3

You are using the simple-websocket package. This package has a list of supported web servers. The error indicates that you are using a web server that is not in the supported list.

Supported web servers are:

  • The Flask dev server (for development purposes only, of course)
  • Gunicorn
  • Eventlet
  • Gevent

From this list, it seems your only choice for a production web server is Gunicorn, since you say that eventlet/gevent won't work for your needs.

The use of Gunicorn is covered in the documentation. In that section, simple-websocket is the third option mentioned. Here is the example start up command:

gunicorn -w 1 --threads 100 module:app
Incurrent answered 18/6, 2021 at 9:43 Comment(1)
Make sure to set debug mode to prevent OSError: [Errno 98] Address already in use. But personally, I am still running into the same problem as earlier (RuntimeError: Cannot obtain socket from WSGI environment. in server, and WebSocket connection to 'ws://localhost:5000/socket.io/?EIO=4&transport=websocket&sid=40NYzDgGYStMR0CEAAAJ' failed: in client)Edwinaedwine
O
0

Maybe you can turn on the logger and the engine.io logger. that qould give you an idea what is the issue.

don't use eventlet and gevent together. use anyone and uninstall the other.

Olivo answered 17/6, 2021 at 12:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.