npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jet-react/react-native-ntp-client

v1.0.1

Published

React Native NTP Client

Downloads

178

Readme

@jet/react-native-ntp-client

React Native compatible implementation of the NTP Client Protocol.

Used to ensure the time used is in sync across distributed systems. The sync is achieved by the following process:

  • Fetches the time from an NTP server.
  • Adjusts for network latency and transfer time
  • Computes the delta between the NTP server and the system clock and stores the delta for later use.
  • Uses all the stored deltas to get the average time drift from UTC.
  • Allows for specifying multiple NTP servers as backups in case of network errors.
  • Ability to get historical details on (un)successful syncs, errors, and raw time values

Installation

npm i @jet/react-native-ntp-client

React Native Compatibility

React Native Version >=0.60.0 - react-native-udp for more information.

Getting Started

  1. Install the module with: yarn add @jet/react-native-ntp-client or npm install @jet/react-native-ntp-client.
  2. Install the native required dependency: yarn add react-native-udp or npm install react-native-udp.
  3. Using React Native >= 0.60, on iOS run pod install in ios directory.

Usage

Import the module into your codebase

import ntpClient from '@jet/react-native-ntp-client';

Create an instance of the clock object passing in the required params. See the options section below for options that can be used.

var options = {};

// create a new instance
var clock = ntp = new ntpSync(options);

// get the current unix timestamp
var currentTime = clock.getTime();

console.log(currentTime);

// manually sync the time
var result = await clock.syncTime();

Options

The client constructor can accept the following options. all options are optional

Basic Options
  • autoSync (boolean) : A flag to control if the client will do automatic synchronizatin. Defaults to true
  • history (+int) : The number of delta values that should be maintained and used for calculating your local time drift. Defaults to 10 if not present, zero, or supplied value is not a number. Supplied value must be > 0
  • servers (array of NtpServer) { server: string; port: number; } : The server used to do the NTP synchronization. Defaults to
[
      { server: "time.google.com", port: 123 },
      { server: "time.cloudflare.com", port: 123 },
      { server: "0.pool.ntp.org", port: 123 },
      { server: "1.pool.ntp.org", port: 123 },
]
  • syncInterval (+number) : The time (in milliseconds) between each call to an NTP server to get the latest UTC timestamp. Defaults to 5 minutes
  • syncOnCreation (boolean) : A flag to control the NTP sync upon instantiation. Defaults to true. (immediate NTP server fetch attempt)
  • syncTimeout (+number) : The timeout (in milliseconds) that will be used in every NTP syncronization . Defaults to 10 seconds
{
  "history": 10,
  "syncOnCreation": false,
  ...
}

Methods

getHistory()

Returns an Object of historical details generated as clockSync runs. It includes several fields that can be used to determine the behavior of a running clockSync instance. Each call represents an individual 'snapshot' of the current clockSync instance. History is not updated when instance is offline.

Fields

  • currentConsecutiveErrorCount (int) : Count of current string of errors since entering an error state (isInErrorState === true). Resets to 0 upon successful sync.
  • currentServer (object) : Object containing server info of the server that will be used for the next sync. Props are:
    • server (string) : the NTP server name
    • port (int) : the NTP port
  • deltas (array<object>) : This array will contain a 'rolling' list of raw time values returned from each successful NTP server sync wrapped in a simple object with the following keys: (note: array length is limited to config.history; oldest at index 0)
    • dt (+/- int) : The calculated delta (in ms) between local time and the value returned from NTP.
    • ntp (int) : The unix epoch-relative time (in ms) returned from the NTP server. (raw value returned from server) note: ntp + -1(dt) = local time of sync
  • errors (array<object>) : This array will contain a 'rolling' list of any errors that have occurred during sync attempts. (note: array length is limited to config.history; oldest at index 0). The object contains typical fields found in JS Errors as well as additional information.
    • name (string) : JavaScript Error name
    • message (string) : JavaScript Error message
    • server (object) : The server that encountered the sync error. Same keys as currentServer object. (possibly different values)
    • stack (string) : JavaScript Error stack trace (if available)
    • time (int) : The local unix epoch-relative timestamp when error was encountered (in ms)
  • isInErrorState (boolean) : Flag indicating if the last attempted sync was an error (true) Resets to false upon successful sync.
  • lastSyncTime (int) : The local unix epoch-relative timestamp of last successful sync (in ms)
  • lastNtpTime (int) : The NTP unix epoch-relative timestamp of the last successful sync (raw value returned from server)
  • lastError (object) : The error info of the last sync error that was encountered. Object keys are same as objects in the errors array.
  • lifetimeErrorCount (int) : A running total of all errors encountered since clockSync instance was created.
  • maxConsecutiveErrorCount (int) : Greatest number of errors in a single error state (before a successful sync).

getTime()

Returns unix timestamp based on delta values between server and your local time. This is the time that can be used instead of new Date().getTime()

syncTime()

An on-demand method that will force a sync with an NTP server. NOTE: You generally do not need to invoke a manual sync since ntpClient automatically runs sync according to the specified syncInterval interval (or its default).

startAutoSync()

An on-demand method that will start auto sync according to the specified syncInterval interval (or its default).

stopAutoSync()

An on-demand method that will stop auto sync according.

Based On

@jet/react0native-ntp-client is based on react-native-clock-sync and react-native-ntp-client by Artem Russkikh

Dependencies