@soundworks/service-sync
soundworks
service dedicated at synchronizing clocks across devicesBasically a wrapper of https://github.com/collective-soundworks/sync
Install
npm install --save @soundworks/service-sync
Usage
Client
registering the service
// index.js
import { Client } from '@soundworks/core/client';
import serviceSyncFactory from '@soundworks/service-sync/client';
const client = new Client();
client.registerService('sync', serviceSyncFactory, {
getTimeFunction: () => new Date().getTime(),
}, dependencies = []);
@soundworks/service-platform
usage in conjonction with If you use an AudioContext
to synchronize audio events accross device, the AudioContext
should be resumed before the synchronization process starts. This can be achieved using the @soundworks/service-platform
.
// index.js
import { Client } from '@soundworks/core/client';
import servicePlatformFactory from '@soundworks/service-platform/client';
import serviceSyncFactory from '@soundworks/service-sync/client';
const client = new Client();
const audioContext = new AudioContext();
client.registerService('platform', serviceSyncFactory, {
features: [
['web-audio', audioContext]
]
}, []);
client.registerService('sync', serviceSyncFactory, {
getTimeFunction: () => audioContext.currentTime,
}, ['platform']); // add 'platform' as a dependency of the sync service
requiring the service
// MyExperience.js
import { Experience } from '@soundworks/core/client';
class MyExperience extends Experience {
constructor() {
super();
// this.platform = this.require('platform');
this.sync = this.require('sync');
}
start() {
const syncTime = this.sync.getSyncTime();
const localTime = this.sync.getLocalTime();
}
}
options
-
getTimeFunction
: function that return a clock to be synchronized with the server (defaults to() => new Date().getTime()
) -
report
: define if the client should report the synchronization reports to the server (defaults tofalse
)
Server
registering the service
// index.js
import { Server } from '@soundworks/core/server';
import serviceSyncFactory from '@soundworks/service-sync/server';
const server = new Server();
server.registerService('sync', serviceSyncFactory, {}, dependencies = []);
requiring the service
// MyExperience.js
import { Experience } from '@soundworks/core/server';
class MyExperience extends Experience {
constructor() {
super();
this.sync = this.require('sync');
}
start() {
const syncTime = this.sync.getSyncTime();
}
connect(client) {}
disconnect(client) {}
}
options
-
getTimeFunction
: function that return a time to be used as the common time (sync time) accross clients and server (default toprocess.hrtime
)
More informations
License
BSD-3-Clause