Javascript

Quick guide for JavaScript library.

Getting Started

Visit the MQTT overview page for conventions, authentication, and more information.

This documentation uses npm MQTT library. This library is open-source, so you can check it out on GitHub.

Install the packages

npm install mqtt --save

Import libraries

Then import it and initialize it with the required packages. Of course, you’ll want to replace DEVICE_ID and DEVICE_TOKENwith your actual device credentials which you can find under your device settings on Qubitro Portal.

var mqtt = require('mqtt')

Define variables

var broker = 'PASTE_HOST_HERE'
var deviceToken  = 'PASTE_DEVICE_TOKEN_HERE'
var deviceId  = 'PASTE_DEVICE_ID_HERE'

Setup MQTT Client

var options = {
    port: 8883,
    keepalive: 60,
    secureProtocol: 'TLSv1_method'
    clientId: deviceId,
    username: deviceId,
    password: deviceToken,
};

var client = mqtt.connect(broker,options)

Prepare Data

const data = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
}

var sdata = JSON.stringclearify(data)

Examples

We strongly recommend using the 8883 port for MQTT over TLS connectivity.

Publish

An example of publishing data.

Subscribe

✨ Support and Feedback

If you have further questions or suggestions, feel free to join πŸ‘‡ -> Qubitro Community Discord via this invitation link.

Last updated