Socket.io-client: Ping-pong for latency doesnt seem to work anymore

Created on 10 Nov 2020  路  6Comments  路  Source: socketio/socket.io-client

You want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

What is actually happening?

Pong event on the client is not fired/triggered, even with pingInterval enabled/set on the server

Steps to reproduce (if the current behaviour is a bug)

https://github.com/Tiboonn/socket.io-fiddle
In the fork above the version of socket.io and socket.io-client are 3.0.0 which the 'pong' event doesnt work on.
If you change both versions in the packege.json to 2.3.0 and reinstall the packages the 'pong' event is working

Expected behaviour

What is expected?

I am expecting to get the latency is ms with the following code block on the client side

socket.on('pong', function (ms) {
    console.log(ms)
})

Setup

  • OS: Ubuntu 20.04
  • browser: Firefox/Chrome (tested multiple)
  • socket.io version: 3.0.0

Other information (e.g. stacktraces, related issues, suggestions how to fix)

Needs documentation

Most helpful comment

Hi! You are absolutely right, this should be included in the migration guide.

You should be able to reproduce the behavior with:

// server-side
io.on("connection", (socket) => {
  socket.on("ping", (cb) => {
    if (typeof cb === "function")
      cb();
  });
});

// client-side
setInterval(() => {
  const start = Date.now();

  socket.volatile.emit("ping", () => {
    const latency = Date.now() - start;
    // ...
  });
}, 5000);

All 6 comments

After reading v3 documentation I noticed pong event was removed but in breaking changes its not mentioned.

Is there any workaround at the moment?

Also this mentioned in following commit:

https://github.com/socketio/socket.io-client/commit/be8c3141bdbbdfa59c4d0ca4b3149236e200878f

Seems this feature was removed due engine.io...

@hadimostafapour Good find!!! :)
I think I will take the code from the commit you mentioned and implement it myself :D

Hi! You are absolutely right, this should be included in the migration guide.

You should be able to reproduce the behavior with:

// server-side
io.on("connection", (socket) => {
  socket.on("ping", (cb) => {
    if (typeof cb === "function")
      cb();
  });
});

// client-side
setInterval(() => {
  const start = Date.now();

  socket.volatile.emit("ping", () => {
    const latency = Date.now() - start;
    // ...
  });
}, 5000);

@darrachequesne Thanks for the code snippet!!

Added here: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/#No-more-%E2%80%9Cpong%E2%80%9D-event-for-retrieving-latency

Thanks for the feedback :+1:

Was this page helpful?
0 / 5 - 0 ratings