Skip to content

moocf/time-server.nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Trying out Network Time Protocol (NTP) on Node.js.


NTP is a protocol for synchronizing clocks over a network. It is used to keep the time on a computer synchronized with a reference time source. The reference time source is usually a radio or satellite transmitter, or a GPS receiver. In our case, we will use our custom NTP server.

Our NTP server is implemented in src/server.mjs. It starts a UDP server on port 1234 by default, and accepts and responds to NTP requests in JSON. An NTP client sends it {originateTimestamp} (t0), and the server responds with {timeError, originateTimestamp, receiveTimestamp, transmitTimestamp} (Δtₛ, t0, t1, t2). Upon receiving the response at {terminateTimestamp} (t3), the client calculates the {roundTripDelay} δ (t3 - t0) - (t2 - t1) and the {timeOffset} θ (t1 + t2)/2 - (t0 + t3)/2 to synchronize its clock. This step is repeated a few times, outliers are removed, and resulting values averaged, to get a more accurate estimate of the time offset. Note that you must include the {timeError} of the NTP server in the error bounds of the time offset, i.e., client time offset is ${AVERAGE(timeOffset)} ± ${timeError + AVERAGE(roundTripDelay)/2}. Our NTP client is implemented in src/client.mjs.


We simulate an NTP synchronication over the local network using (logs-0):

$ node src/server.mjs
# Server listening at 0.0.0.0:1234
# - timeError:  0 ms
# - timeOffset: 0 ms
# - recieveDelay:  0 ms
# - responseDelay: 0 ms
# - transmitDelay: 0 ms
$ node src/client.mjs
# Client got message from 127.0.0.1:1234
# ...
# Client time offset is -0.75 ± 1.5 ms
# Client needs to add 0.75 ms to its clock

We simulate an NTP synchronication over the intracontinential network using a symmetric recieve and transmit delay of 15 ms each (logs-30):

$ node ./src/server.mjs --recieve-delay 15 --transmit-delay 15
# Server listening at 0.0.0.0:1234
# - timeError:  0 ms
# - timeOffset: 0 ms
# - recieveDelay:  15 ms
# - responseDelay: 0 ms
# - transmitDelay: 15 ms
$ node src/client.mjs
# Client got message from 127.0.0.1:1234
# ...
# Client time offset is 6.0625 ± 24.4375 ms
# Client needs to add -6.0625 ms to its clock

We simulate an NTP synchronication over the intercontinential network using a symmetric recieve and transmit delay of 250 ms each (logs-500):

$ node ./src/server.mjs --recieve-delay 250 --transmit-delay 250
# Server listening at 0.0.0.0:1234
# - timeError:  0 ms
# - timeOffset: 0 ms
# - recieveDelay:  250 ms
# - responseDelay: 0 ms
# - transmitDelay: 250 ms
$ node src/client.mjs
# Client got message from 127.0.0.1:1234
# ...
# Client time offset is 0 ± 264.75 ms
# Client needs to add 0 ms to its clock


References




ORG DOI

About

Trying out Network Time Protocol (NTP) on Node.js.

Resources

License

Stars

Watchers

Forks