JavaScript library for interacting with the BIG IoT marketplace
Switch branches/tags
gh-pages greenkeeper/apollo-cache-inmemory-1.1.5 greenkeeper/apollo-cache-inmemory-1.2.0 greenkeeper/apollo-cache-inmemory-1.2.1 greenkeeper/apollo-cache-inmemory-1.2.2 greenkeeper/apollo-cache-inmemory-1.2.3 greenkeeper/apollo-cache-inmemory-1.2.4 greenkeeper/apollo-cache-inmemory-1.2.5 greenkeeper/apollo-cache-inmemory-1.2.6 greenkeeper/apollo-cache-inmemory-1.2.7 greenkeeper/apollo-cache-inmemory-1.2.8 greenkeeper/apollo-cache-inmemory-1.2.9 greenkeeper/apollo-cache-inmemory-1.2.10 greenkeeper/apollo-cache-inmemory-1.3.0 greenkeeper/apollo-cache-inmemory-1.3.5 greenkeeper/apollo-cache-inmemory-1.3.6 greenkeeper/apollo-cache-inmemory-1.3.7 greenkeeper/apollo-cache-inmemory-1.3.8 greenkeeper/apollo-cache-inmemory-1.3.9 greenkeeper/apollo-cache-inmemory-1.3.10 greenkeeper/apollo-cache-inmemory-1.3.11 greenkeeper/apollo-cache-inmemory-pin-1.1.4 greenkeeper/apollo-cache-inmemory-pin-1.1.12 greenkeeper/apollo-cache-inmemory-pin-1.2.10 greenkeeper/apollo-client-2.2.6 greenkeeper/apollo-client-2.3.0 greenkeeper/apollo-client-2.3.1 greenkeeper/apollo-client-2.3.2 greenkeeper/apollo-client-2.3.3 greenkeeper/apollo-client-2.3.4 greenkeeper/apollo-client-2.3.5 greenkeeper/apollo-client-2.3.6 greenkeeper/apollo-client-2.3.7 greenkeeper/apollo-client-2.3.8 greenkeeper/apollo-client-2.4.0 greenkeeper/apollo-client-2.4.1 greenkeeper/apollo-client-2.4.2 greenkeeper/apollo-client-2.4.3 greenkeeper/apollo-client-2.4.4 greenkeeper/apollo-client-2.4.5 greenkeeper/apollo-client-2.4.6 greenkeeper/apollo-client-2.4.7 greenkeeper/apollo-client-pin-2.2.5 greenkeeper/apollo-client-pin-2.2.8 greenkeeper/apollo-client-pin-2.4.2 greenkeeper/apollo-link-1.2.4 greenkeeper/apollo-link-1.2.5 greenkeeper/apollo-link-1.2.6 greenkeeper/apollo-link-http-1.5.3 greenkeeper/apollo-link-http-pin-1.5.2 greenkeeper/apollo-link-pin-1.2.3 greenkeeper/coveralls-3.0.1 greenkeeper/coveralls-pin-3.0.0 greenkeeper/eslint-5.0.0 greenkeeper/eslint-5.0.1 greenkeeper/eslint-config-airbnb-base-13.0.0 greenkeeper/eslint-plugin-import-2.12.0 greenkeeper/eslint-plugin-import-2.13.0 greenkeeper/eslint-plugin-import-pin-2.11.0 greenkeeper/graphql-14.0.0-rc.1 greenkeeper/graphql-14.0.0-rc.2 greenkeeper/graphql-14.0.0 greenkeeper/graphql-14.0.1 greenkeeper/graphql-14.0.2 greenkeeper/graphql-tag-2.9.2 greenkeeper/graphql-tag-pin-2.9.1 greenkeeper/jsonwebtoken-8.2.1 greenkeeper/jsonwebtoken-8.2.2 greenkeeper/jsonwebtoken-8.3.0 greenkeeper/jsonwebtoken-8.4.0 greenkeeper/jsonwebtoken-pin-8.2.0 greenkeeper/jsonwebtoken-pin-8.3.0 greenkeeper/mocha-5.2.0 greenkeeper/mocha-pin-5.1.1 greenkeeper/nyc-11.7.2 greenkeeper/nyc-11.7.3 greenkeeper/nyc-11.8.0 greenkeeper/nyc-12.0.1 greenkeeper/nyc-12.0.2 greenkeeper/nyc-pin-11.7.1 greenkeeper/webpack-4.16.3 greenkeeper/webpack-4.16.4 greenkeeper/webpack-4.16.5 greenkeeper/webpack-4.17.0 greenkeeper/webpack-4.17.1 greenkeeper/webpack-4.17.2 greenkeeper/webpack-4.17.3 greenkeeper/webpack-4.18.0 greenkeeper/webpack-4.19.1 greenkeeper/webpack-4.20.0 greenkeeper/webpack-4.20.1 greenkeeper/webpack-4.20.2 greenkeeper/webpack-4.21.0 greenkeeper/webpack-4.22.0 greenkeeper/webpack-4.23.0 greenkeeper/webpack-4.23.1 greenkeeper/webpack-4.25.0 greenkeeper/webpack-4.25.1 greenkeeper/webpack-4.26.0 greenkeeper/webpack-4.26.1 greenkeeper/webpack-4.27.0 greenkeeper/webpack-4.27.1 greenkeeper/webpack-pin-4.16.2 greenkeeper/webpack-pin-4.19.0 master test-java-cors
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
lib
spec
tools
.eslintrc.json
.gitignore
.travis.yml
README.md
index.js
karma.conf.js
package.json
webpack.config.js

README.md

BIG IoT JavaScript library Build Status Coverage Status Greenkeeper badge

This module provides a JavaScript library for interacting with the BIG IoT marketplace.

Features

  • Discovering offerings from the marketplace
  • Subscribing to an offering and receiving data from the provider
  • Registering an offering in the marketplace
  • Validating the JWT token presented by an offering subscriber
  • Supports Node.js (provider,consumer) and browser (consumer only)

Planned features

  • Unregistering an offering from the marketplace

Installation

Simply install this module with NPM:

$ npm install bigiot-js --save

Usage for consumers

Prerequisites:

  • Log into the BIG IoT Marketplace (or another compatible marketplace instance)
  • Register your company and a new consumer
  • Copy the consumer ID and secret from the marketplace UI

A number of examples are available:

We recomment using Webpack to include bigiot also in browser applications. But you may also use the prebuilt .js file, either:

  • Latest: https://flowhub.github.io/bigiot-js/bigiot.js
  • Versioned: https://flowhub.github.io/bigiot-js/$VERSION/bigiot.js

Authenticating with the marketplace

Once you've completed the above steps, you can use this library. Instantiate a consumer with:

const bigiot = require('bigiot-js');
const consumer = new bigiot.consumer(consumerId, consumerSecret);

Then you need to authenticate your consumer with the marketplace:

consumer.authenticate()
  .then(() => {
    // Code to run after successful authentication
  });

Specifying a CORS proxy (browser)

As of July 2018, the Marketplace API does not allow Cross-Origin-Request-Sharing. To work around this, a CORS proxy like cors-anywhere must be used.

const bigiot = require('bigiot-js');
const marketplace = undefined;
const corsproxy = 'https://mycors.example.org';
const consumer = new bigiot.consumer(consumerId, consumerSecret, marketplace, corsproxy);

Discovering available offerings

You can look up offerings in the marketplace. But for more dynamic applications, it is also possible to discover them based on various criteria.

For example, to discover all parking site offerings, you can do the following:

const query = new bigiot.offering('Parking sites', 'urn:big-iot:ParkingSiteCategory');
// If you don't care about specifics on price and location, you can remove those
delete query.license;
delete query.price;
delete query.extent;

// Then get list of matching offerings
consumer.discover(query)
  .then((matchingOfferings) => {
    // Loop through the offerings can subscribe
  });

Subscribing to a known offering

When you've found a data offering from the marketplace, you need to make a subscription in order to access it.

consumer.subscribe('Offering ID here')
  .then((subscription) => {
    // Now you're subscribed. You can use the subscription details to make calls to the offering
    consumer.access(subscription, inputData);
  });

The input data above is a JSON structure fulfilling whatever input parameters the offering requires.

Note: many Java BIG IoT providers utilize a self-signed invalid SSL certificate. These will be rejected by default. To allow requests to these providers from Node.js, pass a custom https.Agent to Consumer:

const https = require('https');
const options = { httpAgent: new https.Agent({rejectUnauthorized: false}) };
const consumer = bigiot.consumer(consumerId, consumerSecret, null, options); 

This is not possible in web browsers. To workaround, use a CORS proxy.

Usage for providers

Prerequisites:

  • Log into the BIG IoT Marketplace (or another compatible marketplace instance)
  • Register your company and a new provider
  • Copy the provider ID and secret from the marketplace UI

See a simple provider example, and the NoFlo integration in the bigiot-bridge repository.

Authenticating with the marketplace

Once you've completed the above steps, you can use this library. Instantiate a provider with:

const bigiot = require('bigiot-js');
const provider = new bigiot.provider(providerId, providerSecret);

Then you need to authenticate your provider with the marketplace:

provider.authenticate()
  .then(() => {
    // Code to run after successful authentication
  });

Defining your offering

// Instantiate an offering of the desired type
const offering = new bigiot.offering(offeringName, offeringRdfType);

// Define the HTTP endpoint consumers should call on your service
offering.endpoints = {
  uri: 'http://example.net/some/path',
};

// Define the geographical extent of your offering
offering.extent = {
  city: 'Berlin',
};

// Define the input parameters your offering accepts, if any
offering.inputData = [
  {
    name: 'latitude',
    rdfUri: 'http://schema.org/latitude',
  },
  {
    name: 'longitude',
    rdfUri: 'http://schema.org/longitude',
  },
]

// Define the data structure your offering returns when called
offering.outputData = [
  {
    name: "temperature",
    rdfUri: 'http://schema.org/airTemperatureValue',
  }
];

Once you're happy with the offering description, you can register it with the marketplace with:

provider.register(offering)
  .then(() => {
    // Code to run after successful registration
  });

The offering registration is timeboxed and will expire by default in ten minutes, so for persistent offerings you should call the activate method in a timer loop and update the expiration time regularly.

Validating subscriber JSON Web Tokens

Subscribers that make requests to your offering will present a HTTP Bearer token signed with your provider secret. You can validate it with:

provider.validateToken(token)
  .catch((err) => {
    // Give a 403 response because token is invalid or expired
  })
  .then(() => {
    // Token is valid
  });

Enabling browser support in Java Provider

These steps are needed to support direct access from web browsers when using the BIG IoT Java lib in your Provider (not bigiot-js).

  1. Use a well known, trusted SSL certificate. The default in Java Provider is self-signed and cannot be used.
  2. Enable CORS support. ((EmbeddedSpark)provider.getEmbeddedServer()).enableCorsAll();

Verify browser-compatability of offerings

Browsers must have valid SSL and support CORS to be used in browser. The bigiotjs-check-offerings tool can test offerings to verify this.

To check offerings of a specific category

bigiotjs-check-offerings --category urn:big-iot:ParkingSiteCategory

To run for all known categories and output an HTML report

bigiotjs-check-offerings --html report.html