Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-socketio - failed to set "Access-Control-Allow-Origin" response header

I wrote this simple flask-socketio code:

from flask import Flask
from flask_socketio import SocketIO, send

app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'

socketio = SocketIO(app)


@socketio.on('message')
def handle_message(msg):
    print 'Message:' + msg
    send(msg, broadcast=True)


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

When I see chrome network analyzing, I can see the "Access-Control-Allow-Origin" value as null.

According to Flask-socketio documentation: (See API Reference @ http://flask-socketio.readthedocs.io/en/latest/)

Parameters:
...
cors_allowed_origins – List of origins that are allowed to connect to this server. All origins are allowed by default.

Another suggestion I found on searching is using flask-CORS:

app.config['SECRET_KEY'] = 'mysecret'
cors = CORS(app)

socketio = SocketIO(app)

I get the same result.

What is a way to allow Cross-Origin requests with flask-socketio?

Thanks in advance.

like image 845
Billie Avatar asked May 31 '18 21:05

Billie


1 Answers

Below solve it for me. Interaction from Angular app.

socketio = SocketIO(app, cors_allowed_origins="*")
like image 148
Roninio Avatar answered Nov 15 '22 07:11

Roninio