If you are familiar with the IBM IoT Foundation, than you know the protocol used for communication between apps and devices is MQTT.  MQTT is an open protocol ideal for IoT solutions due to its lightweight nature.  It works very well when used by devices that might be running on a battery or my have a low bandwidth connection.  In the past when I have worked with the IBM IoT Foundation I have always used an MQTT library from mqtt.org.  These libraries work fine with the IBM IoT Foundation as long as they support MQTT 3.1.  However the IBM IoT Foundation offers its own set of client libraries for both devices and applications that make using the IBM IoT Foundation a little bit easier for developers.  Currently there are client libraries for Node.js, Java, Python, C#, Embedded C, and mBedC.  So what is the advantage of using the client libraries provided by the IBM IoT Foundation over the client libraries directly from mqtt.org?  One advantage that I am a huge fan of is simplicity of code!  Lets take a look.

Here is some sample code that will allow an application to connect to the IBM IoT Foundation Quickstart service using the open source MQTT Node.js library directly.

var mqtt = require('mqtt');
var clientId = 'a:quickstart:demoapp';
var messagingPort = 1883;
var messagingHost = 'quickstart.messaging.internetofthings.ibmcloud.com';
var client = mqtt.createClient(messagingPort, messagingHost, {"clientId" : clientId});
client.on('connect', function() {
  console.log('connected to iot cloud');
});

Here is some code that does the same thing as above but uses the IBM IoT Foundation Node.js client library.

var Client = require('ibmiotf').IotfApplication;
var config = {
  "id" : "demoapp",
  "org" : "quickstart"
};
var app = new Client(config);
app.connect();
app.on('connect', function() {
  console.log('connected to ibm iotf');
});

Yes the IBM IoT Foundation client library code is 2 lines longer than the MQTT client library, but number of lines is not the only measure of simplicity.  Look at what I didn’t need to know when using the IBM IoT Foundation client library.  I didn’t need to know the URL or port to the Quickstart service MQTT broker, and I didn’t need to know how to construct the client id.  I don’t know about you, but whenever I need to connect to the IBM IoT Foundation I am always searching through documentation to find the right MQTT broker URL and how to construct the client ID properly.  The IBM IoT Foundation client library is even a bigger help when connecting to your own organization in the IBM IoT Foundation.  When using your own organization you need to authenticate with an API key and token as well.  So if you are not already using the IBM IoT Foundation client libraries in your IoT apps I suggest you start doing so soon!


Ryan J Baxter

Husband, Father, Software Engineer