Updated: 03 September 2023

Watson IoT Messaging - Node

Basic connections for connecting to Watson IoT Platform with Node.js making use of the ibmiotf package

Installing Dependencies

Install dependencies with NPM

Terminal window
1
npm install

We use the following dependencies and scripts in our package.json file

1
{
2
"name": "watsoniotmessaging",
3
"version": "1.0.0",
4
"description": "Simple Messaging to Watson IoT Platform for Node with MQTT",
5
"main": "app.js",
6
"scripts": {
7
"client": "node client.js",
8
"app": "node app.js"
9
},
10
"author": "Nabeel Valley",
11
"license": "Apache-2.0",
12
"dependencies": {
13
"dotenv": "^6.1.0",
14
"ibmiotf": "^0.2.41"
15
}
16
}

Credentials

You will need credentials for your Device as well as an API Key for your Application Connection, this can be done by creating a new device as well and creating a new App API Key on Watson IoT Platform

Next create a file called .env with the following contents

1
ORG_ID=orginization_id
2
API_KEY=app_api_key
3
AUTH_TOKEN=app_auth_token
4
DEVICE_TYPE=device_type
5
DEVICE_ID=device_id
6
DEVICE_TOKEN=device_token

Running the Apps

In a terminal window run app.js to monitor events and in a separate window run client.js to send some sample events for you to view, note that app.js will log any events passing through your WatsonIoT service

Application

An application that will log events on a Watson IoT Platform instance

This can be run with

Terminal window
1
npm run app

For our logging application we create a new appClient and connect to our MQTT Broker on Watson IoT Platform by reading our config from the environment and using the ibmiotf package

1
require('dotenv').config()
2
3
var Client = require('ibmiotf')
4
var appClientConfig = {
5
org: process.env.ORG_ID,
6
id: process.env.DEVICE_ID,
7
domain: 'internetofthings.ibmcloud.com',
8
'auth-key': process.env.API_KEY,
9
'auth-token': process.env.AUTH_TOKEN,
10
}
11
12
var appClient = new Client.IotfApplication(appClientConfig)

We then subscribe to device events as follows

1
appClient.connect()
2
// setting the log level to 'trace'
3
// appClient.log.setLevel('trace')
4
appClient.on('connect', () => {
5
// Subscribe to Events from All Devices
6
appClient.subscribeToDeviceEvents()
7
8
// Subscribe to Events from Specific Devices
9
// appClient.subscribeToDeviceEvents(process.env.DEVICE_TYPE)
10
})

Once we have subscribed to our device events we can listen for a specific event or log all events as follows

1
// Log all events
2
appClient.on(
3
'deviceEvent',
4
(deviceType, deviceId, eventType, format, payload) => {
5
console.log(
6
'Device Event from :: ' +
7
deviceType +
8
' : ' +
9
deviceId +
10
' of event ' +
11
eventType +
12
' with payload : ' +
13
payload
14
)
15
}
16
)

Device

This will simply post a message to Watson IoT Platform ten times on a fixed interval then disconnect

The IoT Client app can be run with

Terminal window
1
npm run client

We can then simulate a device by just sending sample data to our Watson IoT Platform instance. We first need to initialize our configuration and create a new deviceClient

1
require('dotenv').config()
2
3
var Client = require('ibmiotf')
4
var iotConfig = {
5
domain: 'internetofthings.ibmcloud.com',
6
org: process.env.ORG_ID,
7
id: process.env.DEVICE_ID,
8
type: process.env.DEVICE_TYPE,
9
'auth-method': 'token',
10
'auth-token': process.env.DEVICE_TOKEN,
11
}
12
13
var deviceClient = new Client.IotfDevice(iotConfig)

Then we connect our device to the Watson IoT Platform simply with

1
deviceClient.connect()
2
deviceClient.log.setLevel('trace')
3
deviceClient.on('connect', startClient)

Once that has been done, we listen for the connect event, which indicates that our connection is on, we also pass in a function called startClient which is defined as below, which will just publish 10 messages to the Watson IoT Platform with the deviceClient.publish function and then disconnect

1
startClient = () => {
2
var publishCount = 0
3
var interval = setInterval(() => {
4
publishCount++
5
console.log(publishCount)
6
deviceClient.publish(
7
'status',
8
'json',
9
JSON.stringify({ text: 'Hello World!', count: publishCount })
10
)
11
if (publishCount == 10) {
12
clearInterval(interval)
13
deviceClient.disconnect()
14
}
15
}, 1000)
16
}