websocket - Socket.io with multiple Node.js hosts, emit to all clients -
websocket - Socket.io with multiple Node.js hosts, emit to all clients -
i new socket.io , trying head around best approach solve issue.
we have 4 instances of node.js app running behind load balancer.
what trying accomplish app post info load balancer url hand if off 1 of instances.
the receiving instance store data, utilize socket.io emit info connected clients.
the issue browser/client can connected single instance @ 1 time.
i trying determine if there way emit clients @ once?
or have clients connect multiple servers using io.connect?
or case redis?
publish/subscribe need here. redis give functionality looking out of box. need create redis client , subscribe update channel on each of app server nodes. then, publish update when post successful (or whatever). finally, have redis client subscribe update chanel , on message emit socketio event:
(truncated brevity)
var express = require('express') , socketio = require('socket.io') , redis = require('redis') , rc = redis.createclient() ; var app = express(); var server = http.createserver(app); var io = socketio.listen(server); server.listen(3000); app.post('/targets', function(req, res){ rc.publish('update', res.body); }); rc.on('connect', function(){ // subscribe update channel rc.subscribe('update'); }); rc.on('message', function(channel, msg){ // util.log('channel: ' + channel + ' msg: ' + msg); var msg = json.parse(msg); io.sockets.in('update').emit('message', { channel: channel, msg: msg }); }); then in js app, hear emitted message:
socket.on('message', function(data){ debugger; // updated info }); of course, introducing new redis server adds single point of failure. more robust implementation may utilize message broker amqp or zeromq or similar networking library provides pub/sub capabilities.
node.js websocket redis socket.io
Comments
Post a Comment