GithubHelp home page GithubHelp logo

socket-ntp's Introduction

socket-ntp

NTP Sync using Socket.io. Allows you to sync clients against a server by calculating the time offset.

Installation

npm install socket-ntp

Requires access to socket.io connections on both the client and the server.

Client usage

On the client, include:

<script src="/javascripts/libs/socket.io.min.js"></script>
<script src="/client/ntp.js"></script>
  var socket = io.connect();
  ntp.init(socket);  

  var offset = ntp.offset(); // time offset from the server in ms 

Server usage

From anywhere that you have access to a socket.io instance.

var ntp = require('socket-ntp');

io.sockets.on('connection', function (socket) {
  ntp.sync(socket);
});

Demo

To start the demo run:

node example/app.js

License

(The MIT License)

Copyright (c) 2012 Calvin French-Owen <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

socket-ntp's People

Contributors

cacheflow avatar calvinfo avatar ilikejames avatar mstdokumaci avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

socket-ntp's Issues

Please update the npm repo with the fix from #1

I'm in the process of writing a book on html game programming and would like to keep all of the libraries on npm, but I have to keep a custom version of this library around because the npm version doesn't work when the client and server have different clocks. Please make a new npm release, thanks!!!

Offset race condition

This occurs when you call ntp.offset() before the offsets array has been populated and you can't properly divide sum by the offsets.length, which results in NaN being returned.

for (var i = 0; i < offsets.length; i++) sum += offsets[i]; sum /= offsets.length;

Latency discrepancy when all clients run in same machine

Hi, I'm using socket-ntp to play videos synchronized in two or more machines. To do that, we schedule videos to start play at the same time based on latency between clients(ntp.latency()).

It works fine if clients run in different machines. Now I need to run all clients on the same machine, but it seems that latency is not the real one between the client and server. If I increase the number of clients in the same machine the latency worst.

I think the latency calculation is not to exactly when you have the server and other clients in the same machine. I tried to increase/decrease both parameters sync interval and clear interval but I had no success.

Does someone have any idea to solve this issue?

Handling clock differences

When there is clock difference between the client server, the offset is wrong.

This can be tested by changing the lib/ntp.js ntp:server_sync to:

socket.emit('ntp:server_sync', { t1     : Date.now() + 8000 /* clock 8 seconds a head from client */,
                                 t0     : data.t0 });

The proposed change is to update the client js file so that the diff is calculated by:

var diff = Date.now() - data.t1 + ((Date.now() - data.t0)/2);

Browserify support

Hi, socket-ntp is currently not browserify compliant. Here is how we can make it compliant:

in package.json, add:

"browser": "client/ntp"

in client/ntp.js:

  if (typeof require === 'function') {
    module.exports = ntp;
  }
  else if (typeof define === 'function' && define.amd) {
    define('ntp', [], function () {
      return ntp;
    });
  } else {
    root.ntp = ntp;
  }

Thanks

Setting the server url

Hi just want a question. What if I want to use this server url: pool.ntp.org to get the ntp time. Can this be done in your socket-ntp?

thanks,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.