This page shows how to connect to the Weble IoT Universal Gateway peers API from a browser or a Node.js application.
The connection always goes through POST /auth, which authenticates the client and returns a fully-initialized peers object connected over WebSocket.
The gateway exposes a POST /auth endpoint that returns the source code of the peers client and socket.io client, plus a session identifier. The browser bootstraps it with a single call and gets back a ready-to-use peer object.
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>Loading API</title></head>
<body>
<script>
function apiLogin(host, username, password, callback){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState != 4) return;
if(xhttp.status == 200){
var data = JSON.parse(xhttp.responseText);
eval(data.connect)(data, callback);
} else {
callback(xhttp.status + ' ' + xhttp.responseText);
}
};
xhttp.open('POST', host + '/auth', true);
xhttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhttp.send(JSON.stringify({ username: username, password: md5(password) }));
}
apiLogin('https://gateway.example.com', 'admin', 'mypassword', function(err, peer){
if(err) return console.error('login failed', err);
peer('supervision').require('gateways', function(err, gateways){
gateways.writeValue('4/7/25', 1, function(err){
if(err) console.error('KNX write failed', err)
})
})
})
</script>
</body>
</html>
The password must be hashed with MD5 before being sent. The transport itself should always be HTTPS.
The same apiLogin flow works from any Node.js application. Call POST /auth over HTTPS, then use the returned peer object exactly as you would in a browser.
var https = require('https')
var crypto = require('crypto')
function md5(s){ return crypto.createHash('md5').update(s).digest('hex') }
function apiLogin(host, username, password, callback){
var body = JSON.stringify({ username: username, password: md5(password) })
var req = https.request(host + '/auth', {
method : 'POST',
headers : { 'Content-Type': 'application/json', 'Content-Length': body.length }
}, function(res){
var data = ''
res.on('data', function(c){ data += c })
res.on('end', function(){
try { data = JSON.parse(data) } catch(e){ return callback(e) }
eval(data.connect)(data, callback)
})
})
req.on('error', callback)
req.write(body)
req.end()
}
apiLogin('https://gateway.example.com', 'admin', 'mypassword', function(err, peer){
if(err) return console.error(err)
peer('supervision').require('system', function(err, system){
system.totalmem(function(err, bytes){
console.log('total memory:', bytes)
})
})
})
Once you have a peer object, every example in the reference section applies as-is.
The main entry points you will use are:
peer('supervision').require('moduleName', callback) — load a modulepeer('supervision').on('eventName', listener) — listen to module eventspeer('supervision').isConnected() — check the link to the gatewaySee Peers protocol for the full primitive set.
The peers client reconnects automatically if the link drops, so you do not need to handle reconnection manually. To shut down explicitly:
peer.disconnect() // closes the underlying connection
peer.removeAllListeners() // detach any handlers you registered
For long-running clients, always remove listeners you registered with module.on(...) when you no longer need them — orphaned listeners are the most common cause of memory leaks. Use peer.listenerCount() to monitor.