Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Replace MQTT Libraries With IBM IoT Client Libraries

DZone's Guide to

Replace MQTT Libraries With IBM IoT Client Libraries

Looking at the client libraries to connect to the IBM IoT Foundation Quickstart service using the MQTT Node.js library.

· IoT Zone
Free Resource

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!

Topics:
ibm ,mqtt ,protocol ,iot ,messaging protocol

Published at DZone with permission of Ryan Baxter, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}