Commutative Replicated Data Types for easy collaborative/distributed systems.
JavaScript Shell
Pull request Compare This branch is 79 commits behind dominictarr:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

CRDT - Commutative Replicated Data Types

a CRDT is a data type designed so that operations on it commute - give the same result indepent of the order in which they are applied.

CRDTs give you eventual consistency for free. it is not necessary to track concurrent changes and use complicated merge algorithms. this module is useful for collaborative/distributed/peer2peer (same things) applications.

Further Reading: A comprehensive study of Convergent and Commutative Replicated Data Types

replicating documents

create two documents,

var Doc = require('crdt').Doc
var A = new Doc()
var B = new Doc()

then pipe them together

var as
(as = A.createStream())

We just replicated two documents with in the same process... this is the idea, but of course, we want to do it on different machines...

notice the pattern is basically the same...

the client side ...

var net = require('net')
var es  = requier('es')
var Doc = require('crdt').Doc

var A = new Doc()

var stream
(stream = net.connect())

and the server ...

var net = require('net')
var es  = requier('es')
var Doc = require('crdt').Doc

var A = new Doc()

net.createServer(function (stream) {


Doc#add(obj = {id: ID, ...})

add a Row to the document initialized to obj obj must have a unique id property. return a Row

Doc#createSet (key, value)

Create a Set a set is a collection of rows defined by a particular value on a particular property.

var cheeses = doc.createSet('type', 'cheese')

key and value must both be strings.

Doc#createSeq (key, value)

same as Doc#createSet except that seqs have a significant order.

Doc#createStream (opts)

create a stream that is used to connect to another Doc instance.

event: doc.emit('create', row)

Emitted when a new Row is created

event: doc.emit('row_update', row)

Emitted when a new Row is updated


an object with in a crdt Doc

Row#set(key, value)

set key to value. if Row#set(obj) is called instead all the keys in obj will update atomically.

This causes a 'change' event to be emitted, and an update message to be sent down the stream. (note, if the stream in not yet connected, that is okay, current state of the document is replicated as soon as the streams are connected.)


get the current value for a key.


return a raw object ready for serialization. this is not a JSON string yet, misleading name, but that is the correct JSON.stringify api.

event: Row.emit('change', changed)

Emitted when a row is changed. this may be the result of a local or a remote update.

changed is the a hash of the fields that have changed.


A collection of Rows within a document.


get the contents of this set as a regular js Array


calls toJSON on the each Row in the set and puts it in an array.


check if a row|id is a member of the set.


get an item in this set, if it exists.

Set#each(iter), Set#forEach(iter)

Iterate over the Rows in the set.


removes a row from the set. sets the set's key, to null. note, if you have multiple sets with the same key, they are mutually exclusive, and adding a node to a different set will remove it from the first one.


just like a Set, but the items are ordered. they will begiven a _sort property.


get the first item in the seq.


get the last item in the seq.


check if a row|id is a member of the seq. (inherited from Set)

Seq#indexOf(id | row)

find the index of the given row or id.


get the item currently at index


push a Row onto the start of the Seq


push a Row onto the end of the Seq


get the number of items currently in the Seq.


remove the last item.


remove the first item.

Seq#before(item, id | row)

insert item before the given row/id.

Seq#after(item, id | row)

insert item after the given row/id.