Node.JS sending wrong data through Socket.IO

I'm using Node.JS and Socket.IO to trade data. I have the following "schema":

  • A "local" Socket.IO server, which have a setInterval of 10 seconds that broadcasts different data in each iteration.
  • A "global" Socket.IO server, which, on a request from HTML clients, receives data from the "local" server (ioLocal variable in the following code is a socket.io-client), modifies the data with the number passed through the HTML client request and sends to the client.

The problem is that the HTML clients are receiving wrong data. Imagine that for a timestamp, the local server broadcasts the message "2". Meanwhile, two different HTML clients ask for data, the first one with "2" and the second one with "3". The expected is that the first one receives "4" (2*2) and the second one receives "6" (2*3), but is not working. Instead, the first one receives "4" and the second one receives "12" (4*3). If I had a third client which sends "2", it would receive "24" (12*2).

I tried to create a different var for each client, use socket.set and socket.get, an array of socket.id's but none of these solutions worked.

What's the problem with this? Is it a scope problem? Thanks for your time.

"Global" Server

var ioGlobal = require('socket.io').listen(9090);

ioGlobal.set('log level', 1);
ioGlobal.enable('browser client minification'); // send minified client
ioGlobal.enable('browser client etag'); // apply etag caching logic based on version number
ioGlobal.enable('browser client gzip'); // gzip the file
ioGlobal.set('transports', [ 'websocket', 'xhr-polling' ]);
ioGlobal.set("polling duration", 10);
ioGlobal.set('sync disconnect on unload', true);

var ioLocal = require('socket.io-client').connect('http://10.0.0.219:9091', {
        'connect timeout' : 1500,
        'reconnect' : true,
        'reconnection delay' : 500,
        'max reconnection attempts' : 20
});
ioGlobal.sockets.on('connection', function(iSocket) {
    debug && console.log('Client '+iSocket.id+' connected.');

    iSocket.on('disconnect', function() {
        debug && console.log("Client "+iSocket.id+" disconnected.");
    });

    iSocket.on('data_rt', function(num) {

        ioLocal.on('data_broadcast', function(data) {

            //do something to data with num
            data = data*num;

            iSocket.emit('rsp_data_rt', data);
        });
    });
});

"Local" Server

var ioLocal = require('socket.io').listen(9091);
ioLocal.sockets.emit('data_broadcast', 2);

HTML Client

<!DOCTYPE html5>
<html>
    <head>
        <title>Socket.IO MultiClient Test</title>

        <script type="text/javascript" src="http://10.0.1.180:3000/socket.io/socket.io.js"></script>
        <script>
            var socket = io.connect('http://10.0.0.219:9090');

            function go(){
                var message = document.getElementById('message').value;
                socket.emit('data_rt', message);
            };

            socket.on('rsp_data_rt', function(num){
                    document.getElementById('result').innerHTML = num;
            })
        </script>

    </head>
    <body>
        <h1>Socket.IO Multiclient</h1>
            Message: <input type="text" id="message"><br />
            <button type="button" onclick='go()'>Send</button>
            Result: <span id="result"></span>
    </body>
</html>