2.0.6 • Published 4 years ago

ubimqtt v2.0.6

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

ubimqtt

Mqtt conveniecy library for using the Ubikampus Mqtt bus. Supports publishing signed content on Mqtt (ES512 signatures only!), subscribing to content signed by a given keypair, and subscribing to content signed by a known publisher whose public key can be found on the Mqtt bus itself.

The signature format is JWS JSON, but the payload is unconventionally publised on Mqtt in the format output by JSON.stringify() without Base64 encoding to keep string payloads human-readable on-the-wire.

Install using npm

npm install ubimqtt

Generate a keypair for testing

mkdir -p ~/.private
# ES512
# private key
openssl ecparam -genkey -name secp521r1 -noout -out ~/.private/ubimqtt-testing-key.pem
# public key
openssl ec -in ~/.private/ubimqtt-testing-key.pem -pubout -out ~/.private/ubimqtt-testing-key-public.pem

Example usage

var UbiMqtt = require("ubimqtt");

var fs = require("fs");
var homedir = require('os').homedir();

var logger = console;


var privateKey= fs.readFileSync(homedir+"/.private/ubimqtt-testing-key.pem");
var publicKey= fs.readFileSync(homedir+"/.private/ubimqtt-testing-key-public.pem");

var mqtt = new UbiMqtt("mqtt://localhost:1883");

mqtt.connect(function(error)
    {
    if (error)
        logger.error(error);

    var onMessage = function(topic, message)
        {
        logger.log("Message received from mqtt server: {topic: \""+topic+"\",message: \""+message+"\"}");
        mqtt.disconnect(function(err)
            {
            if (!err)
                logger.log("Disconnected from Mqtt server");
            });
        };
    mqtt.subscribeSigned("test/test", publicKey, this, onMessage, function(err)
        {
        mqtt.publishSigned("test/test", "testmessage", null, privateKey, function(err)
            {
            if (!err)
                logger.log("Publication made");
            else
                logger.log("Publishing failed: "+err);
            });
        });
    });

Do not edit this README.md file directly, it is autogenerated from the template file README.hbs. Edit the template file README.hbs instead.

To generate this README.md file, execute:

npm run jsdoc

Api documentation

UbiMqtt

Kind: global class

new UbiMqtt(serverAddress, options)

Class for signed Mqtt communications at Ubikampus

ParamTypeDescription
serverAddressstringthe Mqtt server to cennect to
optionsobject
options.silentbooleando not print logs

ubiMqtt.connect(callback)

Connecs to the Mqtt server the address of which was given as a constructor parameter

Kind: instance method of UbiMqtt

ParamTypeDescription
callbackfunctionthe callback to call upon connection or error

ubiMqtt.disconnect(callback)

Disconnects from the Mqtt server

Kind: instance method of UbiMqtt

ParamTypeDescription
callbackfunctionthe callback to call upon successful disconnection or error

ubiMqtt.forceDisconnect(callback)

Immediately disconnect without waiting for ACKs. If called before bus connection is established, connection is canceled.

Kind: instance method of UbiMqtt

ParamTypeDescription
callbackfunctioncalled when disconnect succeeds

ubiMqtt.publish(topic, message, opts, callback)

Publishes a message on the connected Mqtt server

Kind: instance method of UbiMqtt

ParamTypeDescription
topicstringthe Mqtt topic to publish to
messageanythe message to publish
optsobjectthe options to pass to node-mqtt
callbackfunctionthe callback to call upon success or error

ubiMqtt.publishSigned(topic, message, opts, privateKey, callback)

Publishes a signed message on the connected Mqtt server

Kind: instance method of UbiMqtt

ParamTypeDescription
topicstringthe Mqtt topic to publish to
messageanythe message to publish
optsobjectthe options to pass to node-mqtt
privateKeystringthe private key in .pem format to sign the message with
callbackfunctionthe callback to call upon success or error

ubiMqtt.subscribe(topic, obj, listener, callback)

Subscribes to a Mqtt topic on the connected Mqtt server

Kind: instance method of UbiMqtt

ParamTypeDescription
topicstringthe Mqtt topic to subscribe to
objanythe value of "this" to be used whan calling the listener
listenerfunctionthe listener function to call whenever a message matching the topic arrives
callbackfunctionthe callback to be called upon successful subscription or error

ubiMqtt.subscribeSigned(topic, publicKeys, obj, listener, callback)

Subscribes to messages signed by particular keypair on a Mqtt topic on the connected Mqtt server

Kind: instance method of UbiMqtt

ParamTypeDescription
topicstringthe Mqtt topic to subscribe to
publicKeysArray.<string>the public keys of the keypairs the messages need to to be signed with. Only messages signed with these keypairs will invoke the listener
objanythe value of "this" to be used whan calling the listener
listenerfunctionthe listener function to call whenever a message matching the topic and signed with the publicKey arrives
callbackfunctionthe callback to be called upon successful subscription or error

ubiMqtt.subscribeFromPublisher(topic, publiserName, obj, listener, callback)

Subscribes to messages on a Mqtt topic on the connected Mqtt server signed by a known publiser The public key of the publiser is used for recognizing the messages originating from the publisher. The public key of the publisher is fetched from the Mqtt topic publishers/publishername/publicKey and kept up-to-date with the help of a regular Mqtt subscription

Kind: instance method of UbiMqtt

ParamTypeDescription
topicstringthe Mqtt topic to subscribe to
publiserNamestringthe name of the known publisher
objanythe value of "this" to be used whan calling the listener
listenerfunctionthe listener function to call whenever a message matching the topic and signed with the publicKey arrives
callbackfunctionthe callback to be called upon successful subscription or error

© 2019 University Of Helsinki

2.0.6

4 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago