Your First IAP/MQ App

Now it's time to build an application that determines the segment ID (SID) of your target SmartServer.  The SID is required as an element of the topic path in all IAP/MQ API topics.  In general, your application publishes to topics to accomplish a task, and subscribes to specific feed back channel topics to see the results, or monitor parts of the system.  This basic code will exist in all applications you write.

Follow these steps:

  1. In VS code, Select  File > Open Folder  and create a new folder called mySid at a desired location.

  2. Select  File > New File , and follow with  File > Save  to save this file as sid.js in the mySid directory.

  3. If not open already, select  View > Intergrated Terminal  to access the command shell.

  4. From the directory mySid, type  npm init  and respond to the prompts as shown in the following screenshot.  The only essential input is the entry point sid.js.
    This command creates the package.json file.
  5. In the terminal window type  npm install mqtt --save  to pull in mqtt in as a dependency.
    This command creates the node_modules directory and its contents.
  6. Select the Debug icon  in the activity bar, and select the settings icon , which creates and opens mySid/.vscode/launch.json file.   

  7. Edit your launch.json file to match the code sample shown below.  Verify that program (line 11) includes your program file \\sid.js, and add the environment variable DEV_TARGET (line 12), and replace the Xs with your SmartServer's IP address.

        "configurations": [
                "type": "node",
                "request": "launch",
                "name": "Launch Program",
                "program": "${workspaceFolder}\\sid.js",
  8. The following code needs to be added to the file sid.js

    Lets walk through the code in chunks:
    •  Line 1 declares the mqtt object that will be the centerpiece of functionality.

    •  Lines 11-21 create the mqtt client object that connects either:
        - to an external broker relative to the application execution (if the environment
           variable DEV_TARGET is defined), or
        - to a broker running on the same machine, by using the loopback address
           as would occur when the application is installed and running on the SmartServer, 
           where the environment variable DEV_TARGET is not defined.

    •  At Line 26, the application subscribes to the topic glp/0/././SID.  Notice that the subscribe method is called before the connection has been established.    

    •  Two mqtt client events connect and message will fire in order when the client connection to the broker is established, and the SID topic is updated.

    sid.js Source Code
    const mqtt = require('mqtt');
    const TOPIC_SID = 'glp/0/././sid';
    const EVENT_SID = 'sid';
    var sid;
     * environ returns the value of a named environment
     * variable, if it exists, or returns the default
     * value otherwise.
    function environ(variable, defaultValue) {
        return process.env.hasOwnProperty(variable) ?
            process.env[variable] : defaultValue;
     * Set up an MQTT client for the duration of the app lifetime.
    var client = mqtt.connect(
        'mqtt://' + environ('DEV_TARGET', '') + ':1883'
     * Subscribe to the segment ID topic.
      (error) => {
          if (error) {
    // MQTT Connect
        () => {
            console.log ("Connected to broker");
     * MQTT message handler.
        (topic, message) => {
            const payload = JSON.parse(message);
            if (topic === TOPIC_SID) {
                // Assuming the SID topic is a string payload
                if (typeof(payload) === typeof('xyz')) {
                    if (payload.length > 0) {
                        sid = payload;
                        console.log('This SmartServer SID: ' + sid);
                    } else {
                        // We are not provisioned.
                        console.log ('The SmartServer is not provissioned');  
        } // message hadler
    ); // on_message even

  9. Run this application using the VS Code debugger.
    You will see the following output In the debug console (the SID reported in your case will be different):

Congratulations, you have made your first call to the IAP/MQ API.  This code is adapted in the next example that will do slightly more interesting things.

Next Step

Now that you have successfully created an application that interacts with the IAP/MQ API to determine the segment ID of the SmartServer,  we will use this application to create one that interacts with devices managed by the SmartServer.  Continue with Interacting with Devices Using IAP/MQ.