How to set up a simple Node.js WebSocket server on your laptop or embedded device and access it securely, to provide a real time UI.
This app note works for BeagleBone, Mac OS X, Raspberry Pi and other Linux devices running Node.js. Found a bug? Let us know.
$ cd ~$ mkdir nodejs-websocket-server $ cd nodejs-websocket-server
$ npm install --save ws
Install YalerTunnel on BeagleBone, Mac, Raspberry Pi or another device
$ ./yalertunnel server 127.0.0.1:8080 ssl:try.yaler.io:443 RELAY_DOMAIN
A simple WebSocket server returning the message it receives.
$ cd ~/nodejs-websocket-server $ sudo nano echo-server.jsEnter the following Node.js code
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log(message);
ws.send(message);
});
});Save changes with CTRL-X, then Y, then RETURN.
$ node echo-server.js
wss://RELAY_DOMAIN.try.yaler.io/ and check Use secure WebSocket.
A combined Web/WebSocket server pushing server statistics, as found in ws examples.
$ cd ~/nodejs-websocket-server $ npm install --save express
$ sudo nano stat-server.js Enter and save the following Node.js code // (c) 2011 Einar Otto Stangvik, MIT License var WebSocketServer = require('ws').Server , http = require('http') , express = require('express') , app = express(); app.use(express.static(__dirname + '/public')); var server = http.createServer(app); server.listen(8080); var wss = new WebSocketServer({server: server}); wss.on('connection', function(ws) { var id = setInterval(function() { ws.send(JSON.stringify(process.memoryUsage()), function() { /* ignore errors */ }); }, 100); ws.on('close', function() { clearInterval(id); }); });
$ mkdir public $ sudo nano public/index.htmlEnter and save the following HTML
<!DOCTYPE html><!-- (c) 2011 Einar Otto Stangvik, MIT License --> <html> <head> <script> function updateStats(memuse) { document.getElementById('rss').innerHTML = memuse.rss; document.getElementById('heapTotal').innerHTML = memuse.heapTotal; document.getElementById('heapUsed').innerHTML = memuse.heapUsed; } var host = window.document.location.host.replace(/:.*/, ''); var ws = new WebSocket('wss://' + host + '/'); ws.onmessage = function (event) { updateStats(JSON.parse(event.data)); }; </script> </head> <body> RSS: <div id='rss'></div><br> Heap total: <div id='heapTotal'></div><br> Heap used: <div id='heapUsed'></div><br> </body> </html>
$ node stat-server.jshttps://RELAY_DOMAIN.try.yaler.io/ e.g. for the relay domain gsiot-ffmq-ttd5 go to https://gsiot-ffmq-ttd5.try.yaler.io/

How to fix common issues.
If node or npm command not found download and install Node.js from https://nodejs.org/ or, on the lite version of Raspbian, type
$ wget http://node-arm.herokuapp.com/node_latest_armhf.deb $ sudo dpkg -i node_latest_armhf.deb $ node -v
not ok code 0 try sudo npm ...DISCONNECTED is expected, as idle client connections are closed after 30 seconds.Error: listen EADDRINUSE happens if another server is already running on this port.Need more relay domains to serve Web and WebSocket requests on separate ports? Get in touch.
This work by Yaler GmbH is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.