question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 😦

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:28

github_iconTop GitHub Comments

8reactions
islam9commented, May 15, 2016

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); });

6reactions
msenevircommented, Jun 20, 2017

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', () => { ... })
Read more comments on GitHub >

github_iconTop Results From Across the Web

If you have duplicate events on your iCloud Calendar
If you see duplicate calendars or events on your iPhone, iPad, iPod touch, Mac, or PC after you set up iCloud Calendar, follow...
Read more >
Creating a Duplicate Event in Google Calendar - Dito
Creating duplicate events is easy … let's take a look at how to create these duplicate events below. After you have created the...
Read more >
Handling duplicate events - IBM
One possible approach to block duplicate events is to use a string metric to retain the unique event identifiers of the events that...
Read more >
Seeing duplicate events from Calendar Sync
Seeing duplicate events from Calendar Sync. Viewing both the source and target calendars at the same time can create the appearance of duplicates....
Read more >
Duplicate Events in Google Analytics 4 and How to Fix them
Seeing duplicate events in Google Analytics 4? In this article, you will learn the most common reasons for this and how to fix...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found