socket.io: Duplicate events

Here I create primitive example of server with socket.io with such stuff:

  • socket.io > 0.9.9
├─┬ socket.io@0.9.9
│ ├── policyfile@0.0.4
│ ├─┬ redis@0.7.2
│ │ └── hiredis@0.1.14
│ └─┬ socket.io-client@0.9.9
│   ├─┬ active-x-obfuscator@0.0.1
│   │ └── zeparser@0.0.5
│   ├── uglify-js@1.2.5
│   ├─┬ ws@0.4.21
│   │ ├── commander@0.6.1
│   │ ├── options@0.0.3
│   │ └── tinycolor@0.0.1
│   └── xmlhttprequest@1.4.2
└──────────────────── ●

on x86_64 Linux:

Distributor ID: Ubuntu Description: Ubuntu 12.04.1 LTS Release: 12.04 Codename: precise Kernel: 3.2.0-29-generic GCC version: 4.6 (x86_64-linux-gnu)


var io = require('socket.io').listen(1717);

io.sockets.on('connection', function (socket) {
  socket.on('ping', function (data){
      console.log(data);
  });

  socket.on('message', function(data){
      console.log(data);
  });
});

And PHP-client like this:

#!/usr/bin/php -q

<?php
error_reporting(E_ALL);
require(dirname(__FILE__).'ElephantIO/Client.php');

$elephant = new ElephantIO\Client('http://localhost:1717');
$elephant->init(false);

$elephant->emit('ping', array('test' => 'Hello World!'), '');
// $elephant->send(ElephantIO\Client::TYPE_MESSAGE, null, null, 'Hello World!');

Here is the log of that server:

   info  - socket.io started
   debug - client authorized
   info  - handshake authorized KAZbMECxcHQBIgTXynHr
   debug - setting request GET /socket.io/1/websocket/KAZbMECxcHQBIgTXynHr
   debug - set heartbeat interval for client KAZbMECxcHQBIgTXynHr
   debug - client authorized for 
   debug - websocket writing 1::
   debug - websocket received data packet 1:::
   debug - websocket writing 1::
   debug - websocket received data packet 3:::Hello World!
Hello World! 
Hello World!

So, every time php-client sends one message or emits event, it’s duplicated on the server side 😦

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 28

Most upvoted comments

Just ran into this issue as well.

Originally, I was attaching listeners on the client like this:

socket.on('connect', () => {
    socket
        .emit('sendAuthCredentials', { ... })
        .on('user joined', () => { ... })
        .on('user left', () => { ... })
})

But @EduardJS explains it very well:

What you need here is to make sure you attach the events only once, no matter how many re-connections you have

My solution was to take the listeners out of the connect callback, and only leave my auth check (which I do want to run on every connection attempt):

socket.on('connect', () => {
    socket.emit('sendAuthCredentials', { ... })
})

socket
    .on('user joined', () => { ... })
    .on('user left', () => { ... })

Problem is about where you call you message event " socket.on(‘message’) " must not be inside connection scope in your client js file .

io.sockets.on('connection', function (socket) { });

–> This is out of the scope …
socket.on('message', function(data){ console.log(data); });

Make sure you check the debug output, see http://socket.io/docs/logging-and-debugging/ This could rule out that it’s in your own code.