dzm
dzm

Reputation: 23544

Socket.io and Load Balancer

I'm using Socket.io and Node.js and have two instances behind a Stingray load balancer.

The load balancer is setup using Generic Streaming and for the most part, seems to be working fine. However, I am noticing some sporadic behavior.

Basically, there are two instances that an individual may be connected to, if one instance emits to all sockets, the other instance won't see or get those emits.

Does that sound accurate? Would anyone know how to ensure that emits done by either server are sent to clients connected to any server?

Thanks! Dave

Upvotes: 2

Views: 2472

Answers (1)

Ian Jennings
Ian Jennings

Reputation: 2319

I came across a similar problem when developing Mote.io and decided to go with a hosted solution instead of building a load balancer. Dealing with this problem is pretty difficult as you need to sync data across servers or load balance your clients to the same instance to make sure they get all the same messages.

Socket.io won't help much specifically. You would need to implement redis, some other data sync or load balancing app.

PubNub will take care of this as well. The backend is responsible for syncing messages, load balancing, etc at an abstract level so all you do is supply a channel name and PubNub will ensure that all clients in that channel get the message.

Real-time Chat Apps in 10 Lines of Code

enter image description here

Enter Chat and press enter
<div><input id=input placeholder=you-chat-here /></div>

Chat Output
<div id=box></div>

<script src=http://cdn.pubnub.com/pubnub.min.js></script>
<script>(function(){
var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chat';
PUBNUB.subscribe({
    channel  : channel,
    callback : function(text) { box.innerHTML = (''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML }
});
PUBNUB.bind( 'keyup', input, function(e) {
    (e.keyCode || e.charCode) === 13 && PUBNUB.publish({
        channel : channel, message : input.value, x : (input.value='')
    })
} )
})()</script>

Upvotes: 1

Related Questions