Javascript

This manual will help you to get started with Javascript and the MQTT.js library.

The MQTT.js library can be used with node.js as well as in the browser.

Get started

To use the library with node.js install the package using npm:

1
$ npm install mqtt

After that you can require the library in your script:

1
var mqtt = require('mqtt');

To use the same library in the browser you can compile the package with browserify, download it and add it to your project manually or use the precompiled library directly from our CDN:

1
<script src="https://assets.shiftr.io/js/mqtt-2.9.0.js"></script>

The mqtt object is now globally available to all your scripts.

Get started by running the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Note: This line is only required when running in node.js
var mqtt = require('mqtt');

var client = mqtt.connect('mqtt://try:try@broker.shiftr.io', {
  clientId: 'javascript'
});

client.on('connect', function(){
  console.log('client has connected!');

  client.subscribe('/example');
  // client.unsubscribe('/example');

  setInterval(function(){
    client.publish('/hello', 'world');
  }, 1000);
});

client.on('message', function(topic, message) {
  console.log('new message:', topic, message.toString());
});

This example script will connect to the try namespace and send a hello world message every second. If you see the connected client and the messages flowing in the real-time graph, you are ready to go!

In Detail

In the following section, we will look at the above example script line by line.

A detailed description of the libraries API can also be found in repository on GitHub.

You can create and connect a new client using the mqtt.connect function:

1
2
3
var client = mqtt.connect('mqtt://try:try@broker.shiftr.io', {
  clientId: 'javascript'
});
  • The first argument is the MQTT URI which you can get from your namespace on shiftr.io.
  • The second argument is an object with the clientId property, which will be displayed as the connections name in the real-time graph.

When the client has successfully connected the following callback will be executed:

1
2
3
4
client.on('connect', function(){
  console.log('client has connected!');
  // ...
});

After a successful connection you can subscribe to topics:

1
client.subscribe('/example');
  • The first argument is the name of the topic to subscribe.

At any point, you can also unsubscribe from a topic.

1
client.unsubscribe('/example');
  • The first argument is the name of the topic to unsubscribe.

Publish messages to the other clients:

1
client.publish('/hello', 'world');
  • The first argument is the topic to publish the message to.
  • The second argument is the payload of the message.

The following callback will executed when new messages are received:

1
2
3
client.on('message', function (topic, message) {
  console.log('new message:', topic, message.toString());
});

Congratulations, now you are able to work with shiftr.io in your Javascript projects!

Notes

Couple of notes on the usage of the library:

  • The client will automatically try to reconnect if there was an connection problem or the connection has been dropped. If the client has to reconnect, all active subscriptions will be restored.