JavaScript MQTT Client

The Losant JavaScript MQTT Client is a library for Node.js compatible compute modules. The library is open source and available on GitHub.

Installation

The JavaScript MQTT Client is installed using npm:

npm install losant-mqtt

JavaScript MQTT Example

Below is a basic example demonstrating how to connect, send state, and subscribe to commands.

var Device = require('losant-mqtt').Device

// Construct device
var device = new Device({
  id: 'my-device-id',
  key: 'my-app-access-key',
  secret: 'my-app-access-secret',
})

// Connect to Losant.
device.connect()

// Listen for commands.
device.on('command', function(command) {
  console.log('Command received.')
  console.log(command.name)
  console.log(command.payload)
})

// Send temperature once every second.
setInterval(function() {
  device.sendState({ temperature: readAnalogIn() })
}, 1000)

Please refer to the repository for detailed documentation and examples.

Was this page helpful?


Still looking for help? You can also search the Losant Forums or submit your question there.