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

socketio-timesyncer

v1.0.2

Published

NTP-like time synchronization library using Socket.io

Downloads

3

Readme

socketIO-TimeSyncer

NTP-like time synchronization library using Socket.io. SocketIO-TimeSyncer can give you the time offset between a client and a server with a precision of milisecond or microsecond using node Date.now() or the microtime node package.

Installation

npm install socketio-timesyncer

Server usage

You just need to open a Socket.io server and pass the connected client's socket to the TimeSyncerServer object.

import { createServer } from 'http';
import * as express from 'express';
import * as sio from 'socket.io';
import { TimeSyncerServer } from 'socketio-timesyncer';

const server = createServer(express());
server.listen(8080, () => {
    console.log(`Server running on port 8080`);
});
const io = sio(server, {transports: ["websocket"]});
        
// Using a dedicated namespace on the socket server is not an obligation
this.io.of('/timesync').on('connection', (socket: SocketIO.Socket) => {
    console.log(`client connected with socket : ${socket.id}`);
    let timeSyncerServer = new TimeSyncerServer(socket);
});

Client usage

On the client, you need to connect to the server with socket.io and pass that socket to the TimeSyncerClient object. You will then need to manually ask for a

import * as io from 'socket.io-client';
import { TimeSyncerClient } from 'socketio-timesyncer';

const socket = io.connect('http://server-ip:8080/timesync', {transports: ["websocket"]});

Then you can start the client with multiple options :

// Default option implie to manually trigger a sync (you should do it regularly like every second)
socket.on('connect', () => {
    let timeSyncClient = new TimeSyncerClient(socket);
    timeSyncClient.synchronize();
    // Show the time offset in miliseconds
    console.log(timeSyncClient.timeOffset);
});

// For a microsecond precision, you need to specify it in the options
socket.on('connect', () => {
    let timeSyncClient = new TimeSyncerClient(socket, {microTimePrecision: true});
    timeSyncClient.synchronize();
    // Show the time offset in microseconds
    console.log(timeSyncClient.timeOffset);
});

// You can have an automatic trigger and specify the time interval
socket.on('connect', () => {
    let timeSyncClient = new TimeSyncerClient(socket, {autoSync: true, autoSyncTime: 1000});
    // Show the time offset which is computed every specified interval (default: 1000ms)
    console.log(timeSyncClient.timeOffset);
});