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"

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.