New
Apps 6,419Makers 6,403This week 7
Promote your app−30%
7views in
the last 30 days

NbrJS

Minimal aggregate computing (eXchange Calculus) in JavaScript, for Espruino, Node and the browser

Built with Claude Code

by samubura · Unclaimed · from ITItaly · added Oct 29, 2026
no ratings0 comments
NbrJS logo

NbrJS

Aggregate computing in JavaScript, with eXchange Calculus (XC) semantics. One program for the whole network, running in the browser, in Node, and on Espruino boards: swarms of Puck.js, or ESP32 robots talking over Bluetooth or WiFi.

require('nbrjs').globals();

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

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

Website: a course, a playground that also uploads to boards, and an ESP32 guide.

Footprint

  • Code: ~3 KB minified. When installed in a board's Storage, as the playground does, it runs from flash: the core takes about 320 variables, against 475 for the same code uploaded as text (Espruino Linux build).
  • BLE messages: binary, with 2-byte path hashes. A hop gradient is 3 bytes, a gradient inside a branch 6 bytes, and the 23-byte advert fits 5–7 operators.
  • Tests: the suite runs on the Espruino interpreter in CI, on the minified bundle that ships to devices.

API

xc(init, n => ...) XC primitive. n: what each neighbour sent me. Return a field (one value per neighbour), a value (the same for all), or retsend(ret, send)
nbr(v) field of the neighbours' v
rep(init, x => ...) local state across rounds
share(init, n => ...) state shared with the neighbours
branch(c, () => a, () => b) splits the network: the two sides do not see each other (b is optional)
align(key, () => ...) explicit alignment, for loops and dynamic structures
aggregate(name, fn) aligns calls by function: different functions never share messages
id(), sense(name), nbrSense(name) device id, local sensor, per-neighbour sensor (a field)
start(program, config) runs the program on config.target
globals() puts the operators above in the global scope (Espruino has no destructuring)

Fields: .self, .map((v, id) => ...), .zip(other, (a, b) => ...), .reduce((acc, v) => ..., init), .min(), .max(), .sum(). Reductions skip the device itself, so Math.min and Math.max work as reducers.

Control flow. Aggregate code under if, ?:, && or || needs branch: a plain if does not align. The playground and scripts/bundle.js warn where one is missing (src/check.js, needs acorn), but never rewrite your code, so it behaves the same from the Espruino IDE.

Standard library (require('nbrjs/lib').globals(), opt-in): hopsTo(source), distanceTo(source, metric), broadcast(source, value), collectSum(distance, value), isLeader(radius), networkMax(v), networkMin(v), neighbourCount(), rounds(), countdown(n), minId(field).

Targets

All targets take period (ms), sensors(id) (values for sense()) and onResult(value, id).

target where options
ble Puck.js, ESP32 ttl (s), scan (ms of listening per period, 400), minRssi, txPower. No infrastructure; nbrSense('rssi'), and nbrSense('range') estimated in metres
udp ESP32, Node wifi: {ssid, password}, position: () => [x, y], range, port, address. Broadcast over WiFi; with positions, neighbours are those within range
sim browser, Node nodes, layout (random, grid, line), range; nbrSense('range') is the distance
host a board tethered to a PC the PC runs the rounds and simulates the neighbours (demo)

On a board

  • From the playground (Chrome or Edge): Sul dispositivo ↗ installs the library in the board's Storage over Bluetooth or USB, then uploads the program, optionally saved to start at boot.
  • From the Espruino Web IDE: add https://samubura.github.io/nbrjs/modules to Settings → Communications → Module URL, and require('nbrjs') works like any Espruino module.
  • From the command line: node scripts/bundle.js app.js > app.bundle.js inlines the minified modules, ready for the espruino CLI.

Development

npm ci
npm test                 # Node
npm run test:espruino    # same tests on the Espruino interpreter (builds it once into .espruino/)
npm run site             # the website, into _site/

License

Apache-2.0. Keep the NOTICE when redistributing.

Description taken from the repository README.

Ratings

–
0 ratings
5 0
4 0
3 0
2 0
1 0

Rate this app

Sign in to rate

Comments 0

Sign in to comment

No comments yet.

Related apps

#aggregate-computing · #javascript · #iot