0

I'm trying to create an app that uses SocketIO, I'm using Flask-socketio and using Socket.io Client Tool to test the connection of the socket. I've done it as shown in the flask-socketio's documentation but I am not able to connect the socket. I get the following errors while connecting:

Client Error: enter image description here

Server Side Error: enter image description here

The code:

app = Flask(__name__)
CORS(app)
app.config.from_object(Config)
socketio = SocketIO(app)

jwt = JWTManager(app)
db = MongoEngine(app)
api = Api(app)


@app.route('/')
def messageReceived(methods=None):
    if methods is None:
        methods = ['GET', 'POST']
    print('message was received!!!')


# @socketio.on('connect', namespace='/test')
# def test_connect():
#     print('Client connected')
#
#
# @socketio.on('disconnect', namespace='/test')
# def test_disconnect():
#     print('Client disconnected')


@socketio.on('my event')
def handle_my_custom_event(json, methods=None):
    if methods is None:
        methods = ['GET', 'POST']
    print('received my event: ' + str(json))
    socketio.emit('my response', json, callback=messageReceived)


if __name__ == '__main__':
    app.run(debug=True, host='localhost', port=5000)
    socketio.run(app, logger=True, engineio_logger=True)

1 Answer 1

4

The Flask-SocketIO server enforces a same-origin policy by default. You need to configure the origin from your Socket.IO test tool as an allowed origin. For example:

socketio = SocketIO(app, cors_allowed_origins="https://amritb.github.io")
Sign up to request clarification or add additional context in comments.

4 Comments

Hello Miguel i have same issue even after i added cors_allowed_origins="*".
Using socketio = SocketIO(app, cors_allowed_origins="http://amritb.github.io") works for me.
@VishalPatel you must use single quotes
Its due to version not matching on server side and client side.

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.