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

How to use in browser? #12

Open
frederikheld opened this issue Aug 27, 2022 · 9 comments
Open

How to use in browser? #12

frederikheld opened this issue Aug 27, 2022 · 9 comments

Comments

@frederikheld
Copy link

Hey @luk3skyw4lker,

thank you for your great work. I'm looking for high precision time synchronization with JS and this package seems to do exactly this.

However, I was not able to get the client running in the browser. When packing it with Webpack it throws dependency errors. I also tried to serve 'node_modules/ntp-time/src/client.js' through express as a static file but it didn't work either.

Can you please help me to get the client running in the browser?

Thanks in advance
Frederik

@luk3skyw4lker
Copy link
Owner

What are the dependency issues? The package doesn't have any production dependencies, only jest to run tests. I didn't use any other library to build it.

@frederikheld
Copy link
Author

frederikheld commented Aug 27, 2022

Thanks for your quick answer :-) The issue is the missing dgram package for udp connections. I did some more research and there seem to be several issues with that:

  • First of all dgram was previously a separate npm package but is now part of core NodeJS (which is not available in the browser) (source 1, source 2)
  • Webpack <5 did do automatic polyfills for core NodeJS packages but Webpack 5 doesn't. The solution would be to use Webpack <5 (source)
  • But this wouldn't help either as browsers can't do UDP :-| (source)

So this seems to be an unsolvable issue :-P

I actually chose your package for its combination of NTP and UDP because I hoped that this would have the least latency and highest precision of all available time sync packages. Websockets seem to be low latency as well. Would it be possible to use ntp-time with websockets?

@luk3skyw4lker
Copy link
Owner

I don't think that this would be possible, since the communication between NTP servers is done by UDP protocol. But even if you had a server that accepts NTP requests by websockets (which is definitely not the standard), this library has only implemented with UDP packages.

@frederikheld
Copy link
Author

frederikheld commented Aug 28, 2022

Edit: okay nevermind, it's just another Node module that is not available in the browser :-P

Thanks for clarification. I forked your repo and started to replace dgram with socket.io. I'm getting pretty close but there's one thing I can't wrap my head around:

The client gives me:

Uncaught TypeError: data.slice(...).readFloatBE is not a function
    at NTPPacket.parse (packet.js:81:1)
    at parse (websocketClient.js:16:1)
    at Socket.<anonymous> (websocketClient.js:103:1)
    at Emitter.emit (index.mjs:136:1)
    at Socket.emitEvent (socket.js:294:1)
    at Socket.onevent (socket.js:281:1)
    at Socket.onpacket (socket.js:249:1)
    at Emitter.emit (index.mjs:136:1)
    at Manager.ondecoded (manager.js:233:1)
    at Emitter.emit (index.mjs:136:1)

Line 81 in packet.js looks like this:

		packet.rootDelay = data.slice(4, 8).readFloatBE(0) / 2 ** 16;

I did not change anything in packet.js. data that goes into NTPPacket.parse() is an ArrayBuffer with the proper length of 48.

The message that goes into NTPPacket.bufferize() looks like this:

NTPPacket {
  mode: 3,
  leap: 0,
  version: 3,
  stratum: 2,
  poll: 10,
  precision: 0,
  rootDelay: 0,
  rootDispersion: 0,
  referenceId: <Buffer 00 00 00 00>,
  referenceTimestamp: 1661724417,
  originateTimestamp: 2208988800,
  rxTimestamp: 1661724422,
  txTimestamp: 2208988800
}

What am I doing wrong here?

@luk3skyw4lker
Copy link
Owner

You should check the return of the data.slice to see if it's being returned as a instance of the Buffer class, if it's not, you should find some other way of reading the floating bytes out of it.

If you want to, you can create a SocketNTPServer class and make a PR to this repository, I would be glad to review the code and release it into NPM.

@luk3skyw4lker
Copy link
Owner

Besides, the input of the parse function shouldn't be a ArrayBuffer, it should be a Buffer, they're different classes.

@acies
Copy link

acies commented Oct 12, 2022

Hi. Any update on this?
Can WebSockets actually be leveraged to function with generic NTP servers?

@frederikheld
Copy link
Author

I developed my own package that is syncing via WebSockets (but using a simpler protocol than NTP). It's closed source though, as it's gonna be the USP of the service I'm working on ... sorry :-P

@luk3skyw4lker
Copy link
Owner

I can't work in the browser compatibility now, but if anyone has the time to do it I would be glad to review and help. I'm gonna leave this issue open for future references and if nobody comes to it, I'll do it myself when I manage to make some time.

Sorry for being unavailable for it now!

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