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

time-diff-client

v2.0.1

Published

Connects to time-diff-server to calculate time diff with it, useful for time synchronization.

Downloads

1

Readme

time-diff-client

Connects to time-diff-server to calculate time diff with it, useful for time synchronization.

Use case

Let's say you want to run something on multiple clients with very low time difference between them. Start playing audio, for example.

time-diff-client will help you do that by allowing each client to calculate its time difference with the time server.

When all clients know their time difference with the time server you can instruct all clients to do stuff at server time, thus achieving a good level of synchrony.

Install

npm install --save time-diff-client

Usage

time-diff-client comes in two flavors:

  • WebSockets
  • UDP

UDP should be the preferred option because it normally has lower latency than WebSockets. But you can choose whichever is best suited for your use case.

UDP

const TimeClient = require('time-diff-client/udp')

// These are the default values.
const params = {
  port: 3025,
  maxIterations: 150,
  minPrecision: 5, // ms
  debug: true
}
const timeClient = new TimeClient(params)
timeClient
  .init()
  .onDiff((diff) => {
    console.log('TIME DIFF:', diff)
    process.exit()
  })

WebSockets

const TimeClient = require('time-diff-client/ws')

// These are the default values.
const params = {
  timeDiffServer: 'ws://localhost:8001',
  maxIterations: 150,
  minPrecision: 5, // ms
  debug: true
}
const timeClient = new TimeClient(params)
timeClient
  .init()
  .onDiff((diff) => {
    console.log('TIME DIFF:', diff)
    process.exit()
  })

Run test clients

These are test implementation of time-diff-client that connects to a time-diff-server instance and prints time diff to console.

The following params can be passed from the command line:

  • DEBUG: Prints info to console. Default is true.
  • PORT: Client port. Default is 3025. Only available on UDP mode.
  • MAX_ITERATIONS: How many requests will be made to the time server. Default is 150.
  • PRECISION: Min precision we're interested in, expressed in ms. Default is 5.
  • TIME_SERVER_URL: URL to time-diff-server instance. Default is 'ws://localhost:8001'. There's no server for UDP, it uses local broadcast in this mode.

WebSockets client test:

node_modules/.bin/tdc-ws-test

Passing params:

PORT=3000 PRECISION=2 node_modules/.bin/tdc-ws-test

UDP client test:

node_modules/.bin/tdc-udp-test

Passing params:

PORT=3000 PRECISION=2 node_modules/.bin/tdc-udp-test

A better approach if you find yourself using this a lot is to add it to your npm scripts:

"scripts": {
  "tdc-ws-test": "tdc-ws-test",
  "tdc-udp-test": "tdc-udp-test"
}

And then run:

npm run tdc-ws-test

Or:

npm run tdc-udp-test

This commands will work directly if you install time-diff-client globally:

tdc-ws-test and tdc-udp-test.

Test

If you clone this repo and want to check code changes run:

npm test

Or:

npm run test-focus

Also run the test clients to ensure connection to time-diff-server still works OK.

Enjoy!