A p2p platform based on Chord protocol using WebRTC data channels.
In this implementation, the server is used to signal the first connection of a peer to another peer in the network, the next connections between peers are signalled in a distributed manner using routing adhoc between the already connected peers. The server itself also acts a peer in the network (it's the first peer of the network), both server and clients use the same codebase.
$ npm install
$ webpack
Configuration file parameters:
- debug: activate/deactivate verbose logging
- url: address in which the server will be running
- m: the size of the network, where size = 2^m
- sha1: use SHA-1 identifiers or integers
Example:
{
"debug": false,
"url": "http://localhost:8080",
"m": 3,
"sha1": false
}
To start the server:
node server.js config-file.json
config-file.json
is optional, if not defined then default configurations are used (from file config-default.json).
Include webrtc-chord-platform.js
:
<script type="text/javascript" src="path/to/webrtc-chord-platform.js"></script>
Create a chord instance:
var chord = new Chord(options);
options
is an object with configuration parameters, example:
options = {
url: "http://localhost:8080", //server url
debug: false, //activate/deactivate verbose logging
storage: sessionStorage //optional, let's you define the key-value object as long as it uses the setItem/getItem API
}
chord.join(function(id){
//returns the id when joined successfully
});
It's possible to define custom events (syntax like socket.io
)
chord.on('event_name', function(data) {
console.log("This is my callback event_name");
});
It's possible to send data directly to a peer as long as a direct connection exist between the two
chord.getConnections();
chord.connectTo(id, function(event) {
//event
//-yourself - trying to connect to yourself
//-connected - is connected
//-not_connected - is not connected
});
chord.emit('event_name', id_other_peer, data);
Broadcast messages to all network peers:
chord.broadcast(“MESSAGE”);
Register broadcast callback:
chord.on('broadcast', function(data) {
console.log("This is my callback broadcast");
console.log(data);
});
chord.leave();
Stores a value in the network
chord.storeValue(myValue);
Get a value from the network by its hash
chord.getValue(hash, function(value) {
console.log("This is my callback");
});
The client-example/client.html
file is an example of a possible client.
Copyright 2015 Bruno Faria, Instituto de Telecomunicações, Wavecom
Licensed under the GPLv3