ntp-time-sync icon indicating copy to clipboard operation
ntp-time-sync copied to clipboard

Error with example in repo

Open sgrund14 opened this issue 8 months ago • 4 comments

Hello,

Trying to do a basic implementation here, on version 0.5.0

import { NtpTimeSync } from 'ntp-time-sync';

const defaultOptions = {
  // list of NTP time servers, optionally including a port (defaults to 123)
  servers: [
    '0.pool.ntp.org',
    '1.pool.ntp.org',
    '2.pool.ntp.org',
    '3.pool.ntp.org',
  ],

  // required amount of valid samples in order to calculate the time
  sampleCount: 8,

  // amount of time in milliseconds to wait for a single NTP response
  replyTimeout: 3000,

  // defaults as of RFC5905
  ntpDefaults: {
    port: 123,
    version: 4,
    tolerance: 15e-6,
    minPoll: 4,
    maxPoll: 17,
    maxDispersion: 16,
    minDispersion: 0.005,
    maxDistance: 1,
    maxStratum: 16,
    precision: -18,
    referenceDate: new Date('Jan 01 1900 GMT'),
  },
};

const timeSync = NtpTimeSync.getInstance(defaultOptions);

export const getNtpTime = async () => {
  try {
    const time = await timeSync.getTime();
    return time;
  } catch (error) {
    console.error('Error getting NTP time:', error);
    return null;
  }

And getNtpTime fails with this error:

Error: Connection error: Unable to get any NTP response after 3 retries

Am I doing something wrong?

sgrund14 avatar May 06 '25 18:05 sgrund14

~~Experiencing the same error with both getTime().then() and await getTime() on 0.5.0. Occurs in both Safari and Chrome.~~


UPDATE 1:

Whoops, nevermind! Just saw in README:

ℹ️ NTP requires UDP which is not available in a browser context!


UPDATE 2:

Just tested in Hono Cloudflare Workers env w/ nodejs_compat flag, does not work either, same error message w/ 9s hang

JiningLiu avatar May 27 '25 16:05 JiningLiu

@JiningLiu Please consult the Cloudflare docs. UDP might not be supported within workers: https://community.cloudflare.com/t/best-way-for-dns-lookups-by-workers/623544/4

buffcode avatar May 29 '25 09:05 buffcode

@sgrund14 What is your environment?

buffcode avatar May 29 '25 09:05 buffcode

UDP might not be supported within workers

That unfortunately does appears to the case... Thank you!

JiningLiu avatar May 29 '25 12:05 JiningLiu