Run the helloMqtt App on the SmartServer

Now that your application has been tested in VS Code, it is time to install and run the code on your SmartServer. 

You should review lines 7-14 of the helloMqtt.js source code to understand how the application connects to the mqtt broker.  In the last exercise, you defined the environment variable DEV_TARGET in the VS code launch.json file to point to your SmartServer IP address.  If the variable is not defined, the code will use a localhost or the loopback address (127.0.0.1).  If you run the application on the SmartServer, it will connect to the MQTT broker that is part of the SmartServer.  This is how the SmartServer internal applications communicate using MQTT. 

Remember, the ultimate goal is to write applications that run on the SmartServer, and use the IAP/MQ API to access SmartServer resources exposed by the IAP/MQ API topics.

So let's continue:

  1. Use Winscp or other SCP or SFTP client to create the directory /var/apollo/data/apps/hellomqtt as user apollo.  

  2. Use Winscp or other SCP or SFTP client to copy the files helloMqtt.js and package.json created in the previous exercise, into the directory /var/apollo/data/apps/hellomqtt  

  3. From an SSH console of the SmartServer, log in as user apollo, type:  cd /var/apollo/data/apps/hellomqtt

  4. Install your application by typing: npm install  
    This command causes npm to go to the internet and install the mqtt module.  In the previous exercise, you added the mqtt dependency when you ran the command npm install mqtt --save, which updated the package.json file with mqtt as a dependency to the package.  As a result, this npm install on your target SmartServer also loads mqtt from the npm software registry on the internet.

  5. From the directory /var/apollo/data/apps/hellomqtt, type:  node helloMqtt   
    The application subscribes to the topic Dave, and publishes four greetings.  If you still have an instance of the application running in the second SSH console (from the previous exercise), then both consoles running instances of the application will subscribe to the topic Dave, and publish multiple greetings as shown below. (If you don't have a second instance running, you can run another instance on a second console, and you will see the results shown below.)




    While this application does little useful work, it does demonstrate how easy it is to write applications that use mqtt to share simple message data using publish/subscribe mechanisms.

Next Step

Now that you have successfully run Hello World on the SmartServer, you can move on to the next step in this tutorial:  Your First IAP/MQ App.