The peers protocol is the internal P2P transport that lets every component of the Weble IoT Universal Gateway — drivers, logics, the web server, the UI, and remote integrations — share modules and exchange events as if they all lived inside the same Node.js process.
This page documents the client-side primitives you will use when writing code against the API. The wire format itself is an implementation detail and is not covered here.
var peers = require('peers')('myClientName')
The argument is the peer name you advertise on the mesh. It must be unique among the currently-connected peers. By convention:
'supervision' — it owns all the core modules.driver_gateways|<gateway id> (loggers and logics: driver_loggers|<id>, driver_logics|<id>). The web server is supervisionWebServer.POST /auth.The function returned is both an object and a function. As an object it has methods (share, connect, listen, …). As a function it takes a peer name and returns a remote-peer handle:
peers.share('myModule', myObject) // expose myModule to other peers
peers('supervision').require('gateways', cb) // load the 'gateways' module from the 'supervision' peer
// events of a module: subscribe on the module object, e.g. gateways.on('newValue 4/7/25', cb) (see section 5)
External clients never call peers.connect() directly — the connection is set up for you by the bootstrap returned from POST /auth. See Getting started for the full flow. After bootstrap, the peer object you receive is already connected and reconnects automatically if the link drops.
requirepeer('supervision').require('gateways', function(err, gateways){
if(err) return console.error(err)
gateways.writeValue('4/7/25', 1, function(err){
if(err) console.error('write failed', err)
})
})
require does a round-trip to the remote peer to ask for the list of methods of the named module. It returns a local proxy object with the same method names — calling any of them serializes the arguments, sends them over the socket, awaits the remote callback, and invokes your local callback with the result.
A few important things to know:
function(err, ...results){}.'address not found 4/7/25'), not Error objects.peer not connected <peer> (the link is down), function not found (no such method), timeout <peer> <module> <method> (no acknowledgement within 10 s, or no answer activity for 60 s). Some methods have their own limit (writeValue 55 s, readValue about 60 s) and answer timeout.require the same module multiple times — each call returns the same proxy object after the first round trip.require and the new method appears.
shareIf your peer wants to expose its own functions to others on the mesh:
peers.share('temperatureSensor', {
read : function(callback){
callback(null, 21.7)
},
setUnit : function(unit, callback){
// ...
callback(null)
}
})
Any other peer can now require('temperatureSensor', cb) and get a proxy with read and setUnit.
The optional third argument of share is a function called each time a peer requires the module. It receives the id of that peer. It cannot refuse access.
on, once, emit, removeListenerLive updates are emitted by the modules, not by the peer itself. Subscribe on the module object returned by require:
peer('supervision').require('gateways', function(err, gateways){
function onValue(value, value_string, value_date){ console.log('4/7/25 =', value) }
gateways.on('newValue 4/7/25', onValue) // every new value of this address
gateways.once('gatewayState 12', function(state){ }) // next state change of gateway 12
// when you no longer need it (always do this!):
gateways.removeListener('newValue 4/7/25', onValue)
})
peer('supervision').on(...) listens to the peer's own channel, which only carries custom events published with peers().emit(...). The core modules do not publish there: listening to newValue … on peer('supervision') receives nothing.
peers().emit('myCustomEvent', 'arg1', 'arg2') // emit on your own peer channel
Common events of the core modules (listen on the module object):
| Event | Emitted by | Description |
|---|---|---|
newValue <addressKey> |
gateways |
Address value changed |
gatewayState <gatewayId> |
gateways |
Gateway connection state changed |
addressInserted / addressUpdated / addressDeleted |
gateways |
Address lifecycle |
gatewayInserted / gatewayUpdated / gatewayDeleted |
gateways |
Gateway lifecycle |
routeStatus |
gateways |
Route status changed |
loggerInserted / loggerUpdated / loggerDeleted |
loggers |
Logger lifecycle |
subscriptionInserted / subscriptionUpdated / subscriptionDeleted |
loggers |
Subscription lifecycle, see Subscriptions |
alarmRaised / alarmAcknowledged / alarmResolved / … |
alarms |
Alarm lifecycle, see Peers API: alarms |
The peers client itself emits events about its own connections:
peers.on('connect', function(peerName){ ... })
peers.on('connect supervision', function(){ ... }) // shortcut for a specific peer
peers.on('disconnect', function(peerName){ ... })
peers.on('disconnect supervision', function(){ ... })
peers.on('error', function(peerName, err){ ... })
Use the 'connect supervision' shortcut to know when the gateway is reachable for the first time — it is the safest place to start require'ing modules:
peers.connect('/tmp/supervision')
peers.once('connect supervision', function(){
peers('supervision').require('gateways', function(err, gateways){
// ...
})
})
peers.connect('/tmp/supervision') (Windows: \\.\pipe\webleSupervision) is only for processes running on the gateway itself, where the peers module is installed. External clients connect as described in section 2.
peers.connectedPeers() // → ['supervision', 'supervisionWebServer', 'driver_gateways|12', ...]
peer('supervision').isConnected() // → true / false
peers.countListeners() // total active listeners — useful for leak detection
Arguments and return values are serialized automatically. Besides JSON values you can pass Buffer, Date and RegExp objects, and functions.
Error arrives as a plain object { name, message, stack }, not as an Error instance.
Long-running peers clients almost always leak through forgotten event listeners. The best practice is:
on() with removeListener(). If your code path adds a listener, the matching cleanup path must remove it.once() when you only need a single notification.peers.countListeners() in long-running services. A growing number is a leak.'connect supervision' — otherwise reconnects multiply your handlers.function attach(){
peer('supervision').require('gateways', function(err, gateways){
gateways.on('newValue 4/7/25', onValue)
})
}
function detach(){
peer('supervision').require('gateways', function(err, gateways){
gateways.removeListener('newValue 4/7/25', onValue)
})
}
peers.on('connect supervision', attach)
peers.on('disconnect supervision', detach)