I am working on a project where an Angular based app is to communicate with Flask via Socket.IO. my flask code is as follows:
from flask import Flask, render_template
from flask_socketio import SocketIO
from flask_cors import CORS
import json
app = Flask(__name__)
CORS(app)
#app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO(app)
@app.route('/')
def sessions():
return render_template('index.html')
def messageReceived(methods=['GET', 'POST']):
print('message was received!!!')
@socketio.on('my event')
def handle_my_custom_event(json, methods=['GET', 'POST']):
json["user_name"]='Asad'
json["user_input"]='My Server'
print('received my event: ' + str(json))
socketio.emit('my response', json, callback=messageReceived)
if __name__ == '__main__':
socketio.run(app, debug=True, host='localhost', port = 5000)
when I try to load my Angular project, it immediately shows a CORS related error. I have tried installing Moesif Origin & CORS Changer and include CORS to the server code as can be seen but no improvement. The Angular code is taken from [https://tutorialedge.net/typescript/angular/angular-socket-io-tutorial/][1]
however, I am not using the websocket server shown in that tutorial as the server from Flask side is being used:
The error shown is: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/socket.io/?EIO=3&transport=polling&t=NCws9C7. (Reason: CORS request did not succeed). [1]: https://tutorialedge.net/typescript/angular/angular-socket-io-tutorial/