1

I want to send message to client (React JS) from another thread (because there I start basic consuming messages from pika and it blocks Flask app). That's my simple code at client side:

const socket = io.connect('ws://127.0.0.1:5000',{transports: ['websocket'], secure: true, port: '5000'});

useEffect(()=> {
    socket.emit("echo", "echo");
}, [])

socket.on('echoresponse', (data) => {
    console.log(data);
});


socket.on('update_page', (data) => {
    console.log(data);
});

When server receives echo, it answers back and everything work ok. But when I try to emit from send_result_in_thread(), client receives nothing. My server side (missing some rabbitmq pika code)

    clients = []
    app = Flask(__name__)
    socketIo = SocketIO(app, async_mode='eventlet', cors_allowed_origins="*")
    eventlet.monkey_patch()
    app.debug = True
    app.host = 'localhost'
    app.debug = True
    app.host = 'localhost'
    
 def send_result_in_thread(cli, message):
    for client in cli:
            socketIo.emit('update_page', message, room=client)
            print('sending message "{}" to client {}'.format(message.decode("utf-8"), client))
    
@socketIo.on("echo")
def echo( message):
     emit('echoresponse', "I'm alive")
    
@socketIo.on('connect')
  def handle_connect():
      print('Client connected')
      clients.append(request.sid)
    
    
@socketIo.on('disconnect')
def handle_disconnect():
     print('Client disconnected')
     clients.remove(request.sid)
    
if __name__ == '__main__':
    consumer = consumer.Consumer()
    t = threading.Thread(target=consumer.run, name="run", args=(clients, ))
    t.daemon = True
    t.start()
    socketIo.run(app, host='0.0.0.0', port='5000',
                     debug=True)

Another thread:

class Consumer():
    def __init__(self):
        self.clients = []
        #and some more code for rabbitmq

    def got_result(self, ch, method, properties, body):
        print("CLIENTS from thread:" + str(self.clients))
        socketIo.start_background_task(send_result_in_thread, self.clients, body)

    def run(self, clients):
        self.clients = clients
        self.channel.basic_consume(self.queue_name, self.got_result)
        self.channel.start_consuming()

I found similar problem here and used as a reference https://stackoverrun.com/ru/q/12599436 But it doesn't help solving the problem. Client_id is passed to thread successfully, the send_result_in_thread is called but client doesn't receive message. I have no idea why. Thanks in advance.

2
  • Set a breakpoint to see if any connection is made and set your echo to run on the connected event callback. Commented Nov 24, 2020 at 22:49
  • Thanks, I solved it! the problem was different socketIo objects in threads @John Commented Nov 25, 2020 at 9:46

1 Answer 1

2

Finally I've found the problem. Every thread had his own socketIo variable, so message was not send to client. My solution: In main:

t = threading.Thread(target=consumer.run, name="run", args=(clients, socketIo))

In run:

self.socketIo = socketIo

And in got_result:

performer.socketIo.start_background_task(performer.send_result_in_thread, self.clients, self.socketIo, body.decode("utf-8"))
Sign up to request clarification or add additional context in comments.

Comments

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.