Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io emits multiple times

I have got a very basic example. This question has been asked previously multiple times in stack overflow itself but I could not get the right answer so I am going with this basic example.

Server:

var app    = require('express')();
var server = require('http').Server(app);
var io     = require('socket.io')(server);

server.listen(3000);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
  socket.on('chat', function (data) {
    var op = false;
    if( data.id == '1234' ){
       op = 'yes';
    }else{
       op = 'no';
    } 
    socket.emit('result', { hello: op });
  });
});

Client:

<html>
    <body>
        <button onclick="check()">Test Me :-)</button>
        <script src="/socket.io/socket.io.js"></script>
        <script>
          var socket = io.connect('http://localhost:3000');

          var check = function(){

            var data = { id : '234'};
            socket.emit('chat', data);

            socket.on('result', function(data){
                console.log('The data is '+data)
            })
          }
        </script>
    </body>
</html>

When I click the test me button for the first time socket.emit('result', { hello: 'world' }); it is emitted one time. And in the console I am getting this printed:

console.log('The data is '+data)

But when I click once again I get this printed thrice:

console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)

When I click for the third time I get printed six times:

console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data) 

Like this it is multiplying and going.

Can anyone please give me the insight on how to solve this problem. Your help is greatly appreciated. Thanks!

like image 652
sdg Avatar asked Aug 28 '15 07:08

sdg


People also ask

How much traffic can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

Does Socket.IO use a lot of memory?

Socket.IO requires a lot of memory which is a potential problem.

Is Socket.IO full duplex?

info. WebSocket is a communication protocol which provides a full-duplex and low-latency channel between the server and the browser.


1 Answers

I think you are adding more and more listeners to 'result' each call you make to check.

First time click -> call 1 console.log from call 1 listener

Second time click -> call 1 console.log from call 1 listener + call 2 console.log from call 1 listener + call 2 console.log from call 2 listener

Third time -> The previous logs + call 3 console.log from call 1 listener + call 3 console.log from call 2 listener and call 3 console.log from call 3 listener.

Try putting the listener for 'result' out of the function:

<html>
 <body>
    <button onclick="check()">Test Me :-)</button>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:3000');

       socket.on('result', function(data){
            console.log('The data is '+data)
        })
      var check = function(){

        var data = { id : '234'};
        socket.emit('chat', data);
      }
    </script>
 </body>
</html>
like image 77
cesarluis Avatar answered Oct 07 '22 17:10

cesarluis