NbrJS

Aggregate computing in JavaScript. One program for the whole network, from the browser to swarms of Puck.js and ESP32.

Start the course Playground GitHub

One program, the whole network

Every device runs the same function, in rounds, exchanging messages with its neighbours only. These two lines work out how many hops each node is from the nearest source:

Click a node to make it a source, drag it, edit the code: the network adapts by itself.

Why NbrJS

The operators you know

nbr, rep, share, branch, xc, with the eXchange Calculus semantics of ScaFi, FCPP and Collektive, plus a standard library of building blocks.

Plain JavaScript

Programs are ordinary functions, and what you write is what runs on the device. The playground checks your code and warns where an if needs a branch.

Made for small devices

About 4 KB minified, running from flash. Over Bluetooth a gradient takes 3 bytes per message.

From the browser to the board

Simulate, then upload over Bluetooth or USB straight from the playground. Bluetooth needs no infrastructure; WiFi suits robots that know their position.

On a device

With Espruino: write the program, then choose where it runs.

require('nbrjs').globals();

var hops = (src) => share(Infinity, (d) => src ? 0 : d.min() + 1);
var program = () => hops(sense('source'));

start(program, {
  target: 'ble',          // 'udp' for WiFi, 'sim' to simulate
  period: 1000,
  sensors: () => ({ source: BTN1.read() }),
  onResult: (h) => LED1.write(h === 0)
});

In the playground, To device ↗ installs the library on the board and uploads the program. For ESP32 there is a step-by-step guide; with a single Puck.js, try the demo with your PC.

In the browser, load src/core.js and src/sim.js; in Node, require('nbrjs').

Where to start