-2

I have a flask backend and react front end. I am trying to send data to react every time something specific happens from a third party in backend. I have tried using socketio however data only gets sent to front end if I first sent something from react and used @socket.on for example:

@socketio.on('randomevent')
def handle():
  emit("test")
  print('x')

So this code works but when i try to do it without:

def handle():
  socketio.emit("test")

It doesn't get received in frontend. The flask socketio documentation says that this should work however it doesn't work for me. Any help would be appreciated thanks

davidism
  • 121,510
  • 29
  • 395
  • 339
  • What are you sending from the frontend? Are you emitting anything? Did you test your event handler with a python client first? – mostlycryptic Feb 18 '23 at 17:06
  • Im not trying to send any data right now im just trying to make it work and it only works with the first case. Im relatively new to socketio so not too sure what a python client is? – tidekis doritos Feb 18 '23 at 17:30
  • https://flask-socketio.readthedocs.io/en/latest/getting_started.html Did you go through this? – mostlycryptic Feb 18 '23 at 17:51
  • What I understand by your question is that you have to emit an event to a react app based on some activity at the backend, right? – mostlycryptic Feb 18 '23 at 17:52
  • Flask-socketio is event based, so the listener you have right now is to listen for the event randomevent. Whenever the event is triggered, the corresponding event handler decides what to do next. I'm hoping you know how websockets work. – mostlycryptic Feb 18 '23 at 17:55
  • Yea I went through that and yes that’s right. thanks for the explanation – tidekis doritos Feb 18 '23 at 17:59

1 Answers1

-1

I think the best solution for this type of problem where you want to receive information from the backend on a backend event would be

  1. make the backend write in a file (json if you want to match it only for some sessions)
  2. let your frontend make periodic async request (e.g. via ajax) and let the backend check per request if there is something in the file for the session
  3. delete the information from the file where the event was listed
  4. if there is something for the session you make what you want

process / part that writes the event

# everytime this program is executed the session '1' will have an event

with open('events.json', 'w') as f:
    f.write('{"1": ["my_event_1", "my_event_2"]}')

backend check


# you need to set somewhere a unique id in the session ... 

@app.route('/backend_request', methods=['POST'])
def backend_request():
    with open('events.json', 'r') as f:
        events = json.load(f)
    if session['id'] in events:
        return {'events': events[session['id']]}
    else:
        return {'events': []}

frontend function

$.ajax({
  type: "POST",
  url: "/backend_request",
  data: {},
  success: function(data) {
    if (data != {'events': []}) {
        alert('there are some events ...');
    }
}
});
raphiel
  • 711
  • 2
  • 9
  • 24