Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask socketio CORS

I've been trying to send data from flask over socket io. I need to access this data from a different origin, but it is giving a CORS error. I have tried using all kinds of cross origin stuff and none of it has worked. Can somebody help with this.

The view that should be called thought socket io:

from flask.ext.cors import cross_origin
@socketio.on('increment',namespace="/api")
@cross_origin()
def increment(message):
    number += 1;
    emit('number',{'data':number},broadcast=True)

Running the server:

app = Flask(__name__)
cors = CORS(app,resources={r"/api/*":{"origins":"*"}})
socketio = SocketIO(app)
app.debug = True
app.host = '0.0.0.0'

socketio.run(app)
like image 216
user3639005 Avatar asked Mar 21 '15 20:03

user3639005


4 Answers

I solved by following:

socketio = SocketIO(app, cors_allowed_origins="*")
like image 74
Zhang Buzz Avatar answered Sep 17 '22 14:09

Zhang Buzz


I had a similar issue, got it working with this setup:

#you may not need all these options
from flask import Flask, render_template, request
from flask.ext.socketio import SocketIO, emit, join_room, leave_room
from flask.ext.cors import CORS

app = Flask(__name__, template_folder='./', static_folder='./', static_url_path='')
app.config['SECRET_KEY'] = 'some-super-secret-key'
app.config['DEFAULT_PARSERS'] = [
    'flask.ext.api.parsers.JSONParser',
    'flask.ext.api.parsers.URLEncodedParser',
    'flask.ext.api.parsers.FormParser',
    'flask.ext.api.parsers.MultiPartParser'
]
cors = CORS(app,resources={r"/*":{"origins":"*"}})
socketio = SocketIO(app)
socketio.run(app,port=5000,host='0.0.0.0')

You can set up routes as such:

@app.route("/")
def indexRoute():
    return render_template('index.html',version=VER)

And socket requests:

@socketio.on('connect',namespace="/home")
def test_connect():
    print "client connected:",rooms()[0]

On the client side in JS i did the following:

var socket = io.connect('http://' + location.hostname + ':5000/home');
socket.on('connect',function(data) {
    console.log('connected to socket');
});

Also, take a look at this snippet from Flask developers.

like image 36
ierdna Avatar answered Sep 19 '22 14:09

ierdna


For some reason, cors_allowed_origins="*" did not work for me. I had to instead specify the full address of the origin as the following:

socketio = SocketIO(app, cors_allowed_origins=['http://127.0.0.1:5500']) 

The error message that says "<client_address> is not an accepted origin. (further occurrences of this error will be logged with level info)" should indicate which address you should be typing in.

like image 37
Egemen Ertuğrul Avatar answered Sep 20 '22 14:09

Egemen Ertuğrul


Check that you are using the supported version of socket.io in your html file.

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js" integrity="sha512-aMGMvNYu8Ue4G+fHa359jcPb1u+ytAF+P2SCb+PxrjCdO3n3ZTxJ30zuH39rimUggmTwmh2u7wvQsDTHESnmfQ==" crossorigin="anonymous"></script>
like image 33
joseph_dougal Avatar answered Sep 19 '22 14:09

joseph_dougal