Javascript

Getting Started

npm install mqtt --save

Import libraries

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

Basic
TLS
Basic
var options = {
port: 1883,
keepalive: 60,
username: deviceId,
password: deviceToken,
clientId: deviceId,
};
var client = mqtt.connect(broker, options);
TLS
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

Publish

Qubitro MQTT Publish Example JavaScript

Subscribe