Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
A library for developing device and application clients for IBM Internet of Things Foundation
The node.js client is used for simplifying the interacting with the Internet of Things Foundation. The following libraries contain instructions and guidance on using the nodejs ibmiotf node to interact with devices and applications within your organizations.
This client library is divided into three parts, Device, ManagedDevice and Application. The Devices section contains information on how devices publish events and handle commands using the nodejs ibmiotf module, ManagedDevice section contains information on how you can manage the device. More information on device management can be found here.. The Applications section contains information on how applications can use the nodejs ibmiotf module to interact with devices.
This library supports to be loaded in node.js and the browser.
The client code is in the src
folder and the tests are in the test
folder.
All code is written in JavaScript 6 and automatically transpiled to JavaScript 5 for testing and building the production version of the library.
npm install -g babel mocha
npm install
npm run test
- run the tests oncenpm run test:watch
- run the tests in watch modenpm build
- build the client and browser bundlesvar Client = require('ibmiotf');
load iotf-client-bundle.js
or iotf-client-bundle-min.js
from the dist
directory
DeviceClient is device client for the Internet of Things Foundation service. You can use this client to connect to the service, publish events from the device and subscribe to commands.
The constructor builds the device client instance. It accepts an configuration json containing the following :
If you want to use quickstart, then send only the first three properties.
var Client = require("ibmiotf");
var config = {
"org" : "organization",
"id" : "deviceId",
"type" : "deviceType",
"auth-method" : "token",
"auth-token" : "authToken"
};
var deviceClient = new Client.IotfDevice(config);
....
Connect to the Internet of Things Foundation by calling the connect function
deviceClient.connect();
deviceClient.on('connect', function () {
//Add your code here
});
....
After the successful connection to the IoTF service, the device client emits connect event. So all the device logic can be implemented inside this callback function.
By default, all the logs of warn
are logged. If you want to enable more logs, use the log.setLevel function. Supported log levels - trace, debug, info, warn, error.
deviceClient.connect();
//setting the log level to 'trace'
deviceClient.log.setLevel('trace');
deviceClient.on('connect', function () {
//Add your code here
});
....
Events are the mechanism by which devices publish data to the Internet of Things Foundation. The device controls the content of the event and assigns a name for each event it sends.
When an event is received by the IOT Foundation the credentials of the connection on which the event was received are used to determine from which device the event was sent. With this architecture it is impossible for a device to impersonate another device.
Events can be published at any of the three quality of service levels defined by the MQTT protocol. By default events will be published as qos level 0.
Events can be published by using
var deviceClient = new Client.IotfDevice(config);
deviceClient.connect();
deviceClient.on("connect", function () {
//publishing event using the default quality of service
deviceClient.publish("status","json",'{"d" : { "cpu" : 60, "mem" : 50 }}');
//publishing event using the user-defined quality of service
var myQosLevel=2
deviceClient.publish("status","json",'{"d" : { "cpu" : 60, "mem" : 50 }}', myQosLevel);
});
....
When the device client connects, it automatically subscribes to any command for this device. To process specific commands you need to register a command callback function. The device client emits command when a command is received. The callback function has the following properties
var deviceClient = new Client.IotfDevice(config);
deviceClient.connect();
deviceClient.on("connect", function () {
//publishing event using the default quality of service
deviceClient.publish("status","json",'{"d" : { "cpu" : 60, "mem" : 50 }}');
});
deviceClient.on("command", function (commandName,format,payload,topic) {
if(commandName === "blink") {
console.log(blink);
//function to be performed for this command
blink(payload);
} else {
console.log("Command not supported.. " + commandName);
}
});
....
When the device clients encounters an error, it emits an error event.
var deviceClient = new Client.IotfDevice(config);
deviceClient.connect();
deviceClient.on("connect", function () {
//publishing event using the default quality of service
deviceClient.publish("status","json",'{"d" : { "cpu" : 60, "mem" : 50 }}');
});
deviceClient.on("error", function (err) {
console.log("Error : "+err);
});
....
Disconnects the client and releases the connections
var deviceClient = new Client.IotfDevice(config);
deviceClient.connect();
client.on("connect", function () {
//publishing event using the default quality of service
client.publish("status","json",'{"d" : { "cpu" : 60, "mem" : 50 }}');
//publishing event using the user-defined quality of service
var myQosLevel=2
client.publish("status","json",'{"d" : { "cpu" : 60, "mem" : 50 }}', myQosLevel);
//disconnect the client
client.disconnect();
});
....
ApplicationClient is application client for the Internet of Things Foundation service. This section contains information on how applications interact with devices.
The constructor builds the application client instance. It accepts an configuration json containing the following :
If you want to use quickstart, then send only the first two properties.
var Client = require("ibmiotf");
var appClientConfig = {
"org" : orgId,
"id" : appId,
"auth-key" : apiKey,
"auth-token" : apiToken
}
var appClient = new Client.IotfApplication(appClientConfig);
....
Connect to the Internet of Things Foundation by calling the connect function
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
//Add your code here
});
....
After the successful connection to the IoTF service, the application client emits connect event. So all the logic can be implemented inside this callback function.
By default, all the logs of warn
are logged. If you want to enable more logs, use the log.setLevel function. Supported log levels - trace, debug, info, warn, error.
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
//setting the log level to 'trace'
appClient.log.setLevel('trace');
appClient.on("connect", function () {
//Add your code here
});
....
When the application clients encounters an error, it emits an error event.
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
//Add your code here
});
appClient.on("error", function (err) {
console.log("Error : "+err);
});
....
Events are the mechanism by which devices publish data to the Internet of Things Foundation. The device controls the content of the event and assigns a name for each event it sends.
When an event is received by the IOT Foundation the credentials of the connection on which the event was received are used to determine from which device the event was sent. With this architecture it is impossible for a device to impersonate another device.
By default, applications will subscribe to all events from all connected devices. Use the type, id, event and msgFormat parameters to control the scope of the subscription. A single client can support multiple subscriptions. The code samples below give examples of how to subscribe to devices dependent on device type, id, event and msgFormat parameters.
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceEvents();
});
....
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceEvents("mydeviceType");
});
....
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceEvents("+","+","myevent");
});
....
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceEvents("myDeviceType","device01","myevent");
appClient.subscribeToDeviceEvents("myOtherDeviceType","device02","myevent");
});
....
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceEvents("myDeviceType","device01","+","json");
});
....
To process the events received by your subscriptions you need to implement an device event callback method. The ibmiotf application client emits the event deviceEvent. This function has the following properties
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceEvents("myDeviceType","device01","+","json");
});
appClient.on("deviceEvent", function (deviceType, deviceId, eventType, format, payload) {
console.log("Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload);
});
....
By default, this will subscribe to status updates for all connected devices. Use the type and id parameters to control the scope of the subscription. A single client can support multiple subscriptions.
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceStatus();
});
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceStatus("myDeviceType");
});
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceStatus("myDeviceType","device01");
appClient.subscribeToDeviceStatus("myOtherDeviceType","device02");
});
To process the status updates received by your subscriptions you need to implement an device status callback method. The ibmiotf application client emits the event deviceStatus. This function has the following properties
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
appClient.subscribeToDeviceStatus("myDeviceType","device01");
appClient.subscribeToDeviceStatus("myOtherDeviceType","device02");
});
appClient.on("deviceStatus", function (deviceType, deviceId, payload, topic) {
console.log("Device status from :: "+deviceType+" : "+deviceId+" with payload : "+payload);
});
Applications can publish events as if they originated from a Device. The function requires
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
var myData={'name' : 'foo', 'cpu' : 60, 'mem' : 50}
appClient.publishDeviceEvent("myDeviceType","device01", "myEvent", "json", myData);
});
Applications can publish commands to connected devices. The function requires
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
var myData={'DelaySeconds' : 10}
appClient.publishDeviceCommand("myDeviceType","device01", "reboot", "json", myData);
});
Disconnects the client and releases the connections
var appClient = new Client.IotfApplication(appClientConfig);
appClient.connect();
appClient.on("connect", function () {
var myData={'DelaySeconds' : 10}
appClient.publishDeviceCommand("myDeviceType","device01", "reboot", "json", myData);
appClient.disconnect();
});
The API documentation can be found here.
FAQs
A library for developing device and application clients for IBM Watson IoT Platform
We found that ibmiotf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.