Flask-socketio: Cross Origin Policy Issue

Created on 7 Aug 2019  Â·  7Comments  Â·  Source: miguelgrinberg/Flask-SocketIO

Hello ,
I am trying to connect from my localhost to a virtual machine via sockets, it worked before with another vm. But now its telling me its blocked by CORS policy below is a screenshot
image

I am running my code as follows:
socketio.run(app, host='0.0.0.0', port=443,ssl_context=...
the certificate of the vm is imported and added to the certificate manager.

question

All 7 comments

The current release of Flask-SocketIO has stricter requirements with regards to cross-origin requests. See the documentation on the cors_allowed_origins option for more information. In your situation I think adding cors_allowed_origins='https://localhost' will resolve the error.

Do you mean adding this parameter to the socketio.run (…)
or to the io.connect at the client side ?
Sorry to bother

SocketIO(app,cors_allowed_origins="*")
this works for me.

@mrxmamun that works but leaves you exposed to potential CSRF attacks, so use at your own risk.

@aat4 this is an option when you create your SocketIO object. Check the documentation if in doubt.

I'm trying to connect to the flask server from a react app. Flask app and client running on localhost. React app running on port 3000, Flask app on 5000. I'm using this and still nothing:
socketio = SocketIO(app,cors_allowed_origins="*")

Any ideas? is https://www.npmjs.com/package/socket.io incompatible? something in node / react?

@swizzersweet do you have any additional components in your stack that could be altering the CORS headers after they are returned by this package?

@miguelgrinberg Following your guidance, I was able to fix it. I ended destroying and reinstalling all the dependencies, and now my ReactJS app can connect properly. I likely installed something that interfered somehow. 🤷

Was this page helpful?
0 / 5 - 0 ratings

Related issues

j2logo picture j2logo  Â·  3Comments

zuifengwuchou picture zuifengwuchou  Â·  5Comments

huangganggui picture huangganggui  Â·  3Comments

EndenDragon picture EndenDragon  Â·  3Comments

hrmon picture hrmon  Â·  5Comments