Skip to content

Error with example in repo #100

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

Open
sgrund14 opened this issue May 6, 2025 · 4 comments
Open

Error with example in repo #100

sgrund14 opened this issue May 6, 2025 · 4 comments

Comments

@sgrund14
Copy link

sgrund14 commented May 6, 2025

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?

@JiningLiu
Copy link

JiningLiu commented May 27, 2025

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

@buffcode
Copy link
Owner

buffcode commented May 29, 2025

@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
Copy link
Owner

@sgrund14 What is your environment?

@JiningLiu
Copy link

UDP might not be supported within workers

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

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