Bok
Bok

Reputation: 587

flask-socketio events not reaching frontnend

I have created a small webapp, and want to use socketio.

I use socket.io v.3 in my javascript, and flask.socketio v 5.0.1 with enginoio v 4.0.0 and python-socketio v 5.0.4

I can successfully emit events from the client to the server, though i cannot communicate back.

My flask code is:

@socketio.on('submit')
def submit(data):
    print('received message: button click')
    print(data)
    emit('submit',{'complete': 66, 'stage': 'deploying groups'})
    print('data send')

My javascript looks like this:

    var socket = io();
    socket.on('*', function(data) {
      document.getElementById("Project Name").value = 'test'
      });
    socket.on('submit', function(data) {
      document.getElementById("Project Name").value = 'test'
      });

The server side code executes, and i see data send in the flask console log, though the client side never does anything. I tried running the command in the console, and i successfully set the name of the field. I've also tried copy pasting the socket.on definitions into the console, but this did not work either.

If i run this command in the console , i see the defined function:

 socket.listeners("submit");

I would expecft the value of "project name" to update to test

Upvotes: 0

Views: 152

Answers (1)

Bok
Bok

Reputation: 587

I figured out what my problem was, I had another function that also created a socket.

I figured out you should only run var socket = io(); once in your code, not once per function.

My problem was I started making app-> backend work and got stuck when trying to return something, due to reinitiating the socket before I received an answer from the backend.

TLDR; only define the socket once per page, not once per function. Defining it again will reset your session.

Upvotes: 0

Related Questions