The library is contained in a single javascript file. It shouldn't need any dependencies, in either a browser or node.js:
const Negentropy = require('Negentropy.js');
First, you need to create a storage instance. Currently only Vector
is implemented:
let storage = new NegentropyStorageVector();
Next, add all the items in your collection, and seal()
:
for (let item of myItems) {
storage.insert(item.timestamp, item.id);
}
ne.seal();
timestamp
should be a JSNumber
id
should be a hex string,Uint8Array
, or node.jsBuffer
Create a Negentropy object:
let ne = new Negentropy(storage, 50_000);
- The second parameter (
50_000
above) is theframeSizeLimit
. This can be omitted (or0
) to permit unlimited-sized frames.
On the client-side, create an initial message, and then transmit it to the server, receive the response, and reconcile
until complete (signified by returning null
for newMsg
):
let msg = await ne.initiate();
while (msg !== null) {
let response = queryServer(msg);
let [newMsg, have, need] = await ne.reconcile(msg);
msg = newMsg;
// handle have/need (there may be duplicates from previous calls to reconcile())
}
- The output
msg
s and the IDs in thehave
/need
arrays are hex strings, but you can setne.wantUint8ArrayOutput = true
if you wantUint8Array
s instead.
The server-side is similar, except it doesn't create an initial message, there are no have
/need
arrays, and newMsg
will never be null
:
while (1) {
let msg = receiveMsgFromClient();
let [newMsg] = await ne.reconcile(msg);
respondToClient(newMsg);
}
- The
initiate()
andreconcile()
methods are async because thecrypto.subtle.digest()
browser API is async. - Timestamp values greater than
Number.MAX_VALUE
will currently cause failures.