Code Monkey home page Code Monkey logo

bytemind-de / nodejs-client-extension-interface Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 3.0 89 KB

Node.js CLEXI is a Websocket server that extends connected clients with additional features of the OS (e.g. Bluetooth beacon scanning, GPIO control, etc.).

Home Page: https://www.npmjs.com/package/clexi

License: MIT License

Shell 2.02% JavaScript 83.91% HTML 14.07%
websocket bluetooth beacon-scanner node-js duplex-communication client-server raspberry-pi gpio led spi

nodejs-client-extension-interface's Introduction

Node.js CLEXI

Node.js CLEXI is a lightweight client extension interface that enhances connected clients with functions of the underlying operating system using a duplex, realtime Websocket connection.

Currently available extensions (activate via settings):

  • clexi-broadcaster - a simple Websocket message broadcaster
  • clexi-http-events - receives HTTP events via '/event' endpoint and broadcasts them via the Websocket connection
  • ble-beacon-scanner - scans for Bluetooth BLE beacons and broadcasts their data
  • runtime-commands - execute runtime commands
  • gpio-interface - register Raspberry Pi GPIO items (buttons, LEDs, pins, SPI, custom), send and receive data

CLEXI works as a web-server as well and can host the client application if required (e.g. just put the files in the www folder).

Using as node module

Install via npm install clexi (tested under Linux, please see requirements section below) then build your own server like this:

'use strict'
const settings = {
	ssl: false
}
const Clexi = require('clexi')(settings);
Clexi.start();

This will run fastify with Websocket and static file support and expose the CLEXI xtensions. Custom settings can be used to implement additional, self-made xtensions as well, e.g.:

const settings = {
	port: 9000,
	hostname: '0.0.0.0',
	id: 'my-clexi-server-123',
	wwwPath: '/home/pi/clexi-www',
	customXtensionsPath: '/home/pi/clexi-xtensions',
	customXtensions: ['my-xtension']
}

See client and extensions section below to get more info.

Raspberry Pi 4 installation

Requirements:

  • Node.js (v0.9.0+ should use 14 - v0.8.2 was tested with 9.11.2 and 10.15.3)
  • Some packages for Bluetooth etc.: sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev libnss3-tools libcap2-bin openssl procps
  • To install Node packages you might need: sudo apt-get install build-essential
  • SSL certificates for HTTPS (you can use the included script to generate self-signed)

Install Node.js 14

You can use the official script:

curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

Install CLEXI

Clone the repository to the folder 'clexi', enter the folder and run npm install:

git clone https://github.com/bytemind-de/nodejs-client-extension-interface.git clexi
cd clexi
npm install

Decide which hostname you want to use for your server. Default is localhost but I usually prefer raspberrypi.local (default hostname of RPi) to make CLEXI available to all devices in the network.
You can change your hostname via the raspi-config tool.

Optional: Generate some self-signed SSL certificates for your CLEXI server:

bash generate_ssl_cert.sh

The tool will ask you for some info. By pressing RETURN you can keep most of the default values, just for common name choose your hostname (or 'localhost').
If you use SSL make sure to set "ssl": true inside the settings.

Configuration (settings.json)

Next step is to adjust the CLEXI settings. Use a text editor of your choice, e.g. nano:

nano settings.json

Here you can change the default port of your server and set the hostname to the SAME name you used for the SSL certificate (e.g. raspberrypi.local). This is important because you might not be able to reach the server otherwhise.
To load specific extensions ajust the array: "xtensions": [...]. For example if you want to activate runtime commands and GPIO interface add:

"xtensions": [
	...
	"ble-beacon-scanner",
	"runtime-commands",
	"gpio-interface"
]

Run the server

Now you can run your server :-)

sudo node server.js

You should see a confirmation that the server is running and that extensions have been loaded (and hopefully no error ^^).
The sudo command is required for Bluetooth control. If you want to run the server without sudo you have to grant node cap_net_raw privileges:

sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)

Finally to check if everything worked out fine visit the test-page in your browser, e.g. https://raspberrypi.local:8443/index.html (depending on your settings). When using a self-signed SSL certificate this also helps to tell the browser to trust them (usually required once).

Client installation

Clexi.js for browsers

Copy latest Clexi.js library from this repository and include it in your page head, e.g.:

<script type="text/javascript" src="lib/clexi-0.9.0.js" charset="UTF-8"></script>

Make sure your server is running and reachable, then connect like this:

var hostURL = "wss://raspberrypi.local:8443";
ClexiJS.serverId = 'my-clexi-server-123'; 	//must be empty or identical to 'id' entry in server settings
  
ClexiJS.subscribeTo('ble-beacon-scanner', function(e){
	console.log('BLE Beacon event: ' + JSON.stringify(e));
}, function(e){
	console.log('BLE Beacon response: ' + JSON.stringify(e));
}, function(e){
	console.log('BLE Beacon error: ' + JSON.stringify(e));
});
  
ClexiJS.pingAndConnect(hostURL, function(err){
	console.log('server ping failed', err.msg);
}, function(e){
	console.log('connected');	
}, function(e){
	console.log('closed');
}, function(err){
	console.log('error', err.message);
}, function(){
	console.log('connecting');
}, function(welcomeInfo){
	console.log('welcome');
	
	//start BLE beacon scanner
	ClexiJS.send('ble-beacon-scanner', {
		ctrl: "start"
	});
});

For more examples check the www folder of this repository.

Using the 'clexi-http-events' extension

This extension receives events at the 'event' endpoint and broadcasts them via the Websocket connection. The 'event' endpoint accepts HTTP GET and POST requests in the following format (curl example):

#POST example
curl -X POST \
  https://raspberrypi.local:8443/event/myEventName \
  -H 'Content-Type: application/json' \
  -d '{
	"answer": "42"
}'
#GET example
curl -X GET \
  'https://raspberrypi.local:8443/event/myEventName?answer=42'

CLEXI will then broadcast the data as following message object to all Websocket clients:

{"name":"myEventName","data":{"answer":"42"}}

Adding your own extensions

  • Check the xtensions folder for references (it's pretty simple ;-))
  • Build your own extension and put the file in the same folder
  • Open settings.json and add your file to the xtensions array by using the filename without ending, e.g.: my-extension.js -> my-extension
  • Subscribe inside your client app to your extension using the same name
  • Done :-)

Version history

See changelog

nodejs-client-extension-interface's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.