Conductor Chord
An ES6 Chord implementation with extensible message delivery, built with WebRTC-Conductor.
Overview
Standard Usage
If you only need a standard chord implementation, then usage could not be simpler. The following source code will create a client (assuming execution in the browser).
let Chord = require("conductor-chord").Chord;
window.c = new Chord({
debug: true
});
c.join("ws://yourserver.me:7171")
.then(() => {
c.on("connect", () => {})
c.on("lowConnection", () => {})
c.on("disconnect", () => {})
c.on("fileAdded", (key, value) => {})
});
And to run your own server, assuming a suitable WebRTC implementation for Node.js.
const Chord = require("conductor-chord").Chord,
wrtc = require("wrtc");
let c = new Chord({
conductorConfig: {
rtc_facade: wrtc
},
isServer: true,
debug: true
});
Chiefly, the module offers item storage across the network. Presently, items are not replicated across the network due to ownership concerns but this may appear in future versions. To use the default functionality:
c.join(addr);
c.addItem(key, value);
c.lookupItem(key);
c.updateItem(key, value);
c.dropItem(key);
Extension
This variant of chord can be extended with any supporting modules by plugging into its base message delegation system.
c.registerModule(module);
class GenericModule {
constructor (chord) {
this.id = "module_name";
this.chord = chord;
}
delegate (message) {
switch (message.handler) {
case "example":
alert(message.data);
if (notAGoodMessage)
message.pass();
else {
let newMsg = this.chord.newMessage(module, handler, data, dest);
message.reply(newMsg)
}
this.chord.sendNewMessage(module, handler, data, dest)
break;
}
}
}
You may wish to make use of the provided RemoteCallable class, designed around providing a reliable platform for modules based on RPC. The following example should demonstrate most points concerning their usage.
const RemoteCallable = require("conductor-chord").RemoteCallable;
class EchoRPC extends RemoteCallable {
constructor (chord) {
super(chord, "EchoRPC");
this._rcTimeout = 1000;
this._rcRetries = 3;
this._rcCacheDuration = 10000;
chord.registerModule(this);
}
delegate (message) {
if(super.delegate(message))
return;
switch (message.handler) {
case "aThing":
this._thing(message.data.params)
break;
}
}
doAThing (param1, param2) {
let dest = ;
return this.call(dest, "aThing", [param1, param2]);
}
}
Changelog
1.1.2
- "full_server" state removed, operation should now be more correct when the first node is joining.
1.1.1
- Error types returned over RemoteCallable are now cast to string before JSON-stringifying.
1.1.0
- Now exposes RemoteCallable, ID classes to users of the module.
- Adds .dropItem(key) onto Chord items.
- Adds .on("evtName", fn) method to handle events. Currently, will emit "fileAdded", "connect", "disconnect", "lowConnection".
1.0.0