Search code examples
javascriptangularjsnode.jswatson-iot

How to use IBMIoTF for node.js in a WebApplication?


I tested the IBMIoTF in a node.js server and it worked well. IBMIoTF you can find here: https://www.npmjs.com/package/ibmiotf

Now I want to use the IBMIoTF in a WebApplication and I notice this little note in the documentation: https://www.npmjs.com/package/ibmiotf#load-the-library-in-browser

Load the library in browser load iotf-client-bundle.js or iotf-client-bundle-min.js from the dist directory

I also took a look into the http://browserify.org/, but I am not able to get it working.

It is able to load the library in the index.html

<script src="libs/iotf/iotf-client-bundle.min.js"></script>

, but how can I create a object instance in the angular module?

Option 1

I am not able to use require in a WebApplication.

var config = {
                       "org": "THEORG",
                       "id": "IOT_WEB_APPLICATION",
                       "auth-key": "THEKEY",
                       "auth-token": "THETOKEN",
                       "type" : "shared"
               };

var IotClient = require('ibmiotf');
var iotClient = new IotClient.IotfApplication(config);

In this situation I get

angular.js:14110 ReferenceError: require is not defined

Option 2

I also tried to use a object, I found in iotf-client.js file.

  module.exports = {
    IotfDevice: _IotfDevice['default'],
    IotfManagedDevice: _IotfManagedDevice['default'],
    IotfGateway: _IotfGateway['default'],
    IotfManagedGateway: _IotfManagedGateway['default'],
    IotfApplication: _IotfApplication['default']
  };

and did a implementation like this in my controller:

var config = {
               "org": "THEORG",
               "id": "IOT_WEB_APPLICATION",
               "auth-key": "THEKEY",
               "auth-token": "THETOKEN",
               "type" : "shared"
             };
var iotClient = new IotfApplication(config);

Here I get:

angular.js:14110 ReferenceError: IotfApplication is not defined

These options didn't work, but how to create a instance for the IBMIoTF? Can anyone help me?


Solution

  • You need to browserify the ibmiotf as part of your buildprocess:
    1. in your package.json add dependency to ibmiotf npm
    2. do npm install
    3. add a script command to your package.json for browserify/uglify like this

    "scripts": {
    "build": "browserify your.js | uglifyjs -m -c warnings=false > bundle.js"
    }
    
    1. do npm build, this will produce a bundle.js with all your javascript files and the dependencies specified to bundle.js

    2. Include the bundle.js in your web html file. ...<script src="bundle.js"></script>

    3. in "your.js" do something like this

      var config = require(YOURCONFIG); var deviceType = "YOURDEVICETYPE"; var appClient = new client.IotfApplication(config); appClient.connect(); appClient.on("connect", function () { console.log("Connected"); appClient.subscribeToDeviceEvents(deviceType); }); appClient.on("deviceEvent", function (deviceType, deviceId, eventType, format, payload) { console.log("Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload); });