jeudi 4 mars 2021

FlaskSocketIO backend to frontend

I have FlaskSocketIO running on my Flask app. I have have a calculation in the backend that I want to sent to the frontend to display as a progress bar (code below). I want my backend to send data to FlaskSocketIO (which it is) and my frontend to receive that passed data from the backend.

All works excluding the frontend.

SocketIO setup:

socketio = Socketio() # this is global 
socketio.init_app(app, cors_allowed_origins="*", logger=True)

Backend:

# using socketIO-client lib rather than redis
with SocketIO('127.0.0.1', 5000, LoggingNamespace) as so:
        so.emit('progressbar', json_data)

FlaskSocketIO:

@socketio.on("progressbar", namespace='/')
def handle_progressbar(json_data): 
    print("socketio.progressbar: Recieved data%) 
    emit(json_data)

Frontend:

socket.on('progressbar', function(data) {
    console.log(data)
});



Aucun commentaire:

Enregistrer un commentaire