ntp.js
v1.1.2
Published
A robust Network Time Protocol (NTP) client implementation for Node.js applications.
Downloads
12
Maintainers
Readme
NTP.JS
A robust Network Time Protocol (NTP) client implementation for Node.js applications.
Features
- Synchronizes local time with NTP servers
- Configurable pool server and port
- Adjustable time offset and update interval
- Error handling with retry mechanism
- Event-driven architecture
- TypeScript support
Installation
npm install ntp.js
Usage
import NTPClient, { NTP_EVENTS } from 'ntp.js';
const ntpClient = new NTPClient({
poolServerName: 'pool.ntp.org',
port: 123,
updateInterval: 60000, // 1 minute
});
ntpClient.on(NTP_EVENTS.SYNC, (time) => {
console.log('Time synced:', new Date(time));
});
ntpClient.on(NTP_EVENTS.ERROR, (error) => {
console.error('NTP error:', error);
});
ntpClient.begin();
API
Constructor
new NTPClient(options?: NTPClientOptions)
Options
poolServerName
: NTP server hostname (default: 'pool.ntp.org')port
: NTP server port (default: 123)timeOffset
: Additional time offset in milliseconds (default: 0)updateInterval
: Time between sync attempts in milliseconds (default: 60000)maxRetries
: Maximum number of retry attempts (default: 3)
Methods
begin()
: Start the NTP client and initiate synchronizationforceUpdate()
: Force an immediate time synchronizationgetTime()
: Get the current synchronized timegetSyncStatus()
: Get the current synchronization statussetTimeOffset(offset: number)
: Set a new time offsetsetUpdateInterval(interval: number)
: Set a new update intervalstop()
: Stop the NTP client and close the UDP connection
Events
'sync'
: Emitted when time is successfully synchronized'error'
: Emitted when an error occurs'syncStatus'
: Emitted when sync status changes'synced'
: Emitted when time is synced'syncing'
: Emitted when sync process starts
License
MIT
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
Author
wraith4081