planet

collaboratively edit anything

Planet

Collaboratively edit JSON-style data in realtime using Socket.IO by synchronizing all operations on a planetary shared object. Each operation will be received in exactly the same order as they are incoming to the Planet server. This includes the client that is emitting the operation. This approach guarantees the exact same state on all clients and has been proven to work reliably in other projects such as netpd.

Planet is optimized to edit JSON style data and does not require OT. If you are looking for rich text editing have a look at ShareJS.

var planet = require('planet'),
    socket = require('socket.io').listen(8080);
 
planet(socket);
planet --host localhost --port 8080
io.connect('//:8080')
.on('connect', function(){
    this.emit('merge', {
        'sugar': 1,
        'milk': 0
    });
 
    this.on('set', function(keyvalue){
        console.log(key, value);
        // sugar 2 
        // milk 100 
    });
 
    this.emit('set', 'sugar', 2);
    this.emit('set', 'milk', 100);
 
    this.emit('get', function(data){
        console.log(data);
        // {'sugar': 2, 'milk': 100} 
    });
});

Or run the example with:

node test/example
  • set - Sets a value at a specific location. The value will be overwritten, not merged!

  • remove - Deletes a value at a specified location.

  • merge - Recursively merges data into the state.

  • delete - Deletes the state.

  • get - Asynchronously fetches values from the state, optionally at a specified location. Returns the whole state if no location is passed.

  • operation - The custom events that is used to modify the planetary shared object.

  • value - Can be of type string, number, object, array, boolean or null.

  • location - Specifies a property of the shared object by a key (string) or path (array).

  • path - A path is an array of strings or/and numbers to specify a property in an object. Numbers refer to element positions of arrays.

  • data - Refers always to an object.

  • state - The current content of the planet that can be manipulated by the operations or read with get.

Arrays are not treated as objects and will not be merged by merge operations. The elements of an array can be set or fetched by get opernations. Removing single elements from an array is not yet specified.

Single characters of a string value can be manipulated with set or read with get.

npm install planet
<script src="//localhost:8080/socket.io/socket.io.js"></script>
// or within Node.js  
var io = require('socket.io-client');

Planet Operations are fired as Socket.IO custom events. The operations can be listened on both the server and the client via on and once.

client.on('set', function(locationvalue){ });
client.on('remove', function(location){ });
client.on('merge', function(data){ });
client.on('delete', function(){ });
var earth = io.connect('//:8004', options);
earth.disconnect();

Emits Planet operations.

earth.emit('set', 'bag', null); // {'bag': null} 
earth.emit('set', 'bag', {'sugar': 20}); // {'bag': {'sugar': 20}} 
earth.emit('set', ['bag', 'eggs'], 12); // {'bag': {'sugar': 20, 'eggs': 12}} 
earth.emit('set', ['todo-list', 0], 'My first thing todo');
earth.emit('remove', 'key');
earth.emit('remove', ['bag', 'eggs']);
earth.emit('merge', {'bag': {'eggs': 6, 'milk': 100}});
earth.emit('merge', {'bag': {'sugar': 20}});
earth.emit('delete');
earth.emit('get', function(data){ });
earth.emit('get', 'bag', function(value){ });
earth.emit('get', ['bag', 'eggs'], function(value){ });
earth.emit('get', ['todo-list', 0], function(value){ });
var Planet = require('planet'),
    socket = require('socket.io').listen(8080, 'localhost');
var earth = new Planet(socket, options);

The new keyword is optional.

  1. Socket - Socket.IO socket server.
  2. Options (object) - the configuration object.
  • limit - the maximum amount of concurrent client connections. Defaults to 200.
earth.merge({'key': 'value'});
earth.set('bag', {'sugar': 20});
earth.set(['bag', 'eggs'], 12);
earth.set(['todo-list', 0], 'My first thing todo');
earth.remove('key');
earth.remove(['bag', 'eggs']);
earth.delete();
earth.get(function(data){ });
earth.get('bag', function(value){ });
earth.get(['bag', 'eggs'], function(value){ });
earth.get(['todo-list', 0], function(value){ });

To run planet form a command-line interface install Planet globally npm install planet -g or cd bin && ./planet.

  • -p, --port [NUMBER] - The port to bind to (default: 8004).

  • -h, --host [STRING] - The host to connect to (default: 127.0.0.1).

  • -l, --limit [NUMBER] - Maximum concurrent client connections, a number lower than your ulimit (default: 200).

  • --io.<configuration> - Socket.IO configuration for example: --io.transports=websocket,htmlfile --no-io.browser-client-cache Note the dot notation and that dashes after --io. will be replaced by whitespace to match Socket.IO configs.

  • -v, --version - Prints the current version.

  • --help - Shows this help message.

#test server api
make test-server
 
#test client api
make test-client
 
#build browser test
make test-browser
 
#run test server
node test/server
node test/benchmark
  • Strict (option for disallowing auto-creation of setting keys at new location)
  • Predefined model (option for predefining a data structure and disalow merging/setting inexistent keys)
  • Latency optimization
  • Cleanup error messages
  • Eventually provide a client side script for merge, get and set manipulation