Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

SocketIO (docker replicas) + Nginx + Browser = dulicated emit events by total of replicas

I have a SocketIO server run inside docker swarm + Nginx for load balancing which is working fine.

But I have a small problem with duplicated emit events.

Example: I have 30 replicas (copies) of SocketIO server; 1 Nginx gateway and Everything running in Docker Swarm

My Nginx config:

server {
listen 80;

server_name  10.10.10.212;

location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_pass http://loadbalancer;
    # enable WebSockets
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    }
 }

My SocketIO server emit

socketio.emit('warehouse_scan:added', 'HB10790')

Browser result

30 results

How can I solve this?

question from:https://stackoverflow.com/questions/65558288/socketio-docker-replicas-nginx-browser-dulicated-emit-events-by-total-of

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

duplicated events on emit is my false. I set wrong parameter while setting up everything with nginx , match client versions , etc ...

Wrong config in backend:

socketio = SocketIO(app, logger=True, engineio_logger=True, policy_server=True, async_mode='eventlet', manage_session=True, cors_allowed_origins="*", message_queue=socket_io_msg_q)

The correct one that fixed duplication:

socketio = SocketIO(app, logger=True, engineio_logger=True, policy_server=True, async_mode='eventlet', manage_session=True)

The problem is at this line:

message_queue=socket_io_msg_q <-- error!

This will trigger all emit events from backend server to REDIS then get back to client. If i have 50 replicas, clients will receive the amount of message from these replicas.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...