Chord Socket

This is an extension of the MeshSocket which syncronizes a common Object(). It works by providing some extra handlers to store data. This exposes the entire dict API.

Note

This is a fairly inefficient architecture for read intensive applications. For large databases which are infrequently changed, this is ideal. For smaller networks where there is significant access required, you should use the sync socket.

Basic Usage

There are three limitations compared to a normal Object().

  1. Keys must be translatable to a Buffer()
  2. Keys are automatically translated to a Buffer()
  3. Fetching values is significantly slower than for a Object()

The only API differences between this and MeshSocket() are for access to this dictionary. They are as follows.

get()

A value can be retrieved by using the get() method. These calls are about O(log(n)) hops, as they approximately halve their search area with each hop.

> let foo = sock.get('test key', null);
> console.log(foo)
Promise { <pending> }
> foo.then(console.log)  // prints the value, if it exists, or ``null``
null

It is important to note that keys are all translated to Buffer() before being used, so it is required that you use a string() or Buffer()-like object.

set()

A value can be stored by using the set() method. Like the above, these calls are about O(log(n)) hops, as they approximately halve their search area with each hop.

> sock.set('test key', 'value');  // Both of these calls are okay
> sock.set(new Buffer('test key'), new Buffer('value'));

Like above, keys and values are all translated to Buffer() before being used, so it is required that you use a string() or Buffer()-like object.

del()

This deletes an association. Like the above, this call is about O(log(n)).

> sock.del('test')

update()

The update method is simply a wrapper which updates based on a fed Object(). Essentially it runs the following:

> for (var key of update_dict)  {
... sock.set(key, update_dict[key]);
... }

keys() / values() / items()

These methods are analagous to the ones in Python’s dict. There are three main differences:

  1. They emulate the Python 3 behavior. So, they will still return an generator, rather than a list.
  2. values() will return a generator of Promise() s
  3. items() will return a generator of Buffer() Promise() pairs

pop() / popitem()

These methods are also analagous to the ones in Python’s dict. The main difference is that if the leasing system is active, calling this method may throw an error if you don’t “own” whatever key is popped.

Events

In addition to the above, and those of MeshSocket(), the ChordSocket() object has two Event() s.

First there’s ChordSocket Event 'add'(). This is called whenever an association is added. Because the value is not necessarily stored by you, it is not given as an argument to this event.

> sock.on('add', (conn, key)=>{
... // conn is a reference to the socket
... console.log(`${key} was added`);
... });

This class has one other event: ChordSocket Event 'delete'(). This is called every time an association is removed.

> sock.on('delete', (conn, key)=>{
... console.log(`The association with key ${key} was deleted`);
... });

Advanced Usage

Refer to the mesh socket tutorial

Use In A Browser

Refer to the mesh socket tutorial