Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket NodeJS localhost connected over server (High Latency) #2274

Closed
1 task done
mahir-zeroday opened this issue Jan 26, 2025 · 3 comments
Closed
1 task done

Websocket NodeJS localhost connected over server (High Latency) #2274

mahir-zeroday opened this issue Jan 26, 2025 · 3 comments

Comments

@mahir-zeroday
Copy link

Is there an existing issue for this?

  • I've searched for any related issues and avoided creating a duplicate issue.

Description

Hello,
I have a question about whether this is a bug or an unusual situation.
I'm sending ping requests from NextJS (localhost:3000) to NodeJS (localhost:8080). I create the ping request myself and send the current time using Date.now() to port 8080 and get it back.
I subtract the timestamp in the message from Date.now() when I receive the message and divide by 2.
This creates a difference of around 13~18ms. I think this difference is quite high for websocket communication between two ports on localhost.
Can I get information about this situation? Are these numbers normal?

ws version

8.18.0

Node.js Version

22.0.0

System

System:
OS: macOS 15.1.1
CPU: (11) arm64 Apple M3 Pro
Memory: 99.66 MB / 18.00 GB
Shell: 3.2.57 - /bin/sh

Expected result

No response

Actual result

No response

Attachments

No response

@lpinca
Copy link
Member

lpinca commented Jan 27, 2025

The numbers are unusual but it has nothing to do ws. I think it is more a network issue. Below is an example in my environment.

// server.mjs
import { WebSocketServer } from 'ws';

const websocketServer = new WebSocketServer({ port: 8080 }, function () {
  console.log('Server listening on port *:8080');
});
// client.mjs
import WebSocket from 'ws';

const ws = new WebSocket('ws://localhost:8080');

ws.on('open', function () {
  ws.ping(Date.now());
});

ws.on('pong', function (data) {
  const time = +data.toString();
  console.log(`Round-trip time: ${Date.now() - data} ms`);

  setTimeout(function () {
    ws.ping(Date.now());
  }, 500);
});
$ node client.mjs
Round-trip time: 2 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 0 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 1 ms
Round-trip time: 0 ms
Round-trip time: 1 ms
...

@mahiraltinkaya
Copy link

Thank you @lpinca ;
I examined your example code and applied it for testing. I'm getting similar results. I think I'm running into some browser or Next.js related factors here. Thank you for your interest.

@lpinca
Copy link
Member

lpinca commented Feb 6, 2025

I'm closing this as answered.

@lpinca lpinca closed this as completed Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants