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.
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"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.