Skip to content

MQTT Stream Client

Giovanni Foiani edited this page Apr 5, 2016 · 1 revision

A Live Stream is a handy way to aggregate channels together. Through its simple interface you can pick available data sources (i.e. device’s channels) and pack data in an unified stream. This stream can then be accessed in real time in order to read available messages.

Import MQTT Stream Client

var MqttStreamClient = require('spacebunny').MqttStreamClient;

Connection parameters

Prerequisites: you must have created a device through the Space Bunny's web interface. You also have at least one enabled channel, e.g. 'data' (name is not mandatory, but we'll use this for our example). See our Getting Started for a quick introduction to Space Bunny's base concepts. Once everything is set up get your device's API key from Space Bunny's web application: on the web interface, go to devices section and create or pick an existing device. Click on the 'SHOW CONFIGURATION' link, copy the API key and substitute it here:

var connectionParams = {
  client: 'your-client-id',
  secret: 'your-secret',
};

You can also provide the endpointUrl key to use a different endpoint, default points to our Demo Application.

You can also provide full manual configuration, all information can be retrieved from the 'SHOW CONFIGURATION' view on Space Bunny web interface. With the following configuration the SDK will not contact the endpoint for configurations.

var connectionParams = {
  client: 'your-client-id',
  secret: 'your-secret',
  host: 'hostname',
  port: 1883, // default for MQTT
  vhost: 'organization-id',
  channels: [ 'data', 'alarms' ]
};

Secure connection

If you want to connect using an encrypted channel, you must enable SSL and provide the client and CA certificates path. Default port for AMQPS connections is 8883. Certificates paths are not mandatory, if not provided the platform just creates an encrypted communication channel without peer verification.

var connectionParams = {
  client: 'your-client-id',
  secret: 'your-secret',
  ssl: true,
  ca: '/path/to/ca_certificate.pem',
  cert: '/path/to/client_certificate.pem',
  key: '/path/to/client_key.pem'
};

Instantiate the client

var streamClient = new MqttStreamClient(connectionParams);

At this point the SDK is auto-configured and ready to use. Configurations are automatically fetched by the SDK.

Receive messages

A stream client can listen for messages coming from multiple hooks, each hook has different parameters:

  • stream: is the stream name. See details on Space Bunny online documentation
  • cache (default true): when is true the stream client is bound to an existing queue that contains a single copy of the last N messages. Attaching multiple clients to the same cached stream can help to consume messages at a higher rate. When cache is false, stream messages are received in a new temporary queue, parallel to the stream cache queue. Multiple parallel temporary queues can be attached to the same stream, it can be useful when the same data stream is addressed to different consumers.
  • callback: function called when a message is received
// callback called when a message is received
var messageCallback = function(content, field, properties) {
  console.log(content);
};
var streamHooks = [
  { stream: 'stream-name', callback: messageCallback },
  { stream: 'stream-name-2', cache: false, callback: messageCallback }
];
var streamClient = new MqttStreamClient(connectionParams);
streamClient.streamFrom(streamHooks).then(function(res) {
  console.log(res);
}).catch(function(reason) {
  console.error(reason);
});