Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node socket.io client listens to events sent to the other client

Node socket.io client listens to events sent to the other client

Problem

I built a simple Node socket.io server, and two simple Node socket.io clients. Each client connects to the server, sends a single request, waits for the response, and prints it. The problem is, each response is printed twice, because each client gets not only his own response, but also the response of the other client! Why?

Here is the server code:

var io = require('socket.io').listen(10001);
io.sockets.on('connection', function (socket) {
  socket.on('echo', function (data) {
    console.log("got request to echo "+data);
    socket.emit("echoes", data+" "+data);
  });
});

Here is the client code:

var socketioclient = require('socket.io-client');
var socket1 = socketioclient.connect(HOST, {port: PORT}); 
var socket2 = socketioclient.connect(HOST, {port: PORT});

socket1.on('connect', function () { console.log("socket1 connected!"); });
socket2.on('connect', function () { console.log("socket2 connected!"); });

socket1.on('echoes', function (result) { console.log("socket1 received: "+result);  });
socket2.on('echoes', function (result) { console.log("socket2 received: "+result);  });

socket1.emit('echo', "aaa");
socket2.emit('echo', "bbb");

Here is the client output:

socket1 connected!
socket2 connected!
socket1 received: aaa aaa
socket2 received: aaa aaa
socket1 received: bbb bbb
socket2 received: bbb bbb

And here is the server log:

   info  - socket.io started
   debug - client authorized
   info  - handshake authorized niJGX0EXWvLPw2THZOeD
   debug - setting request GET /socket.io/1/websocket/niJGX0EXWvLPw2THZOeD
   debug - set heartbeat interval for client niJGX0EXWvLPw2THZOeD
   debug - client authorized for 
   debug - websocket writing 1::
got request to echo aaa
   debug - websocket writing 5:::{"name":"echoes","args":["aaa aaa"]}
got request to echo bbb
   debug - websocket writing 5:::{"name":"echoes","args":["bbb bbb"]}
   debug - emitting heartbeat for client niJGX0EXWvLPw2THZOeD
   debug - websocket writing 2::
   debug - set heartbeat timeout for client niJGX0EXWvLPw2THZOeD
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client niJGX0EXWvLPw2THZOeD
   debug - set heartbeat interval for client niJGX0EXWvLPw2THZOeD
   debug - emitting heartbeat for client niJGX0EXWvLPw2THZOeD
   debug - websocket writing 2::
   debug - set heartbeat timeout for client niJGX0EXWvLPw2THZOeD
Problem courtesy of: Erel Segal-Halevi

Solution

By default, the socket.io client will reuse existing connections from the same client to the same host/port (doc). You can disable this behaviour using the force new connection option:

// client.js
var socket1 = socketioclient.connect(HOST, {port: PORT, 'force new connection': true });
var socket2 = socketioclient.connect(HOST, {port: PORT, 'force new connection': true });
Solution courtesy of: robertklep

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node socket.io client listens to events sent to the other client

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×