bigcat / openwhisk_mqtt_feed

MQTT feed for OpenWhisk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

openwhisk_mqtt_feed

MQTT package for OpenWhisk, provides a topic subscriber feed.

Messages received on the broker topic will be forwarded as trigger activiations.

This project provides the feed action, used to handle trigger registration and deletion events, along with the MQTT subscriber service. This backend service handles subscribing to MQTT messages for registered topics and executing the registered triggers when messages arrive.

usage

$ wsk trigger create mqtt_trigger --feed /{USER_NAMESPACE}/mqtt/mqtt_feed -p topic 'TOPIC' -p url 'mqtt://BROKER_URL'

This feed is currently available for testing as a shared package on IBM Bluemix's OpenWhisk environment: (/james.thomas@uk.ibm.com_dev/mqtt/mqtt_feed)

deployment

Installing a new feed provider requires you to deploy the backend service, register the package and create the feed action. Users must have an account with an OpenWhisk instance and followed the instructions to setup their CLI. Cloudant is used to persist trigger registration state by the backend service. Users need to register an account with this service provider.

initialise database

Create this database in the CouchDB instance used by the application.

  • topic_listeners

Add the following Views to the subscriptions design document.

{
  "_id": "_design/subscriptions",
  "views": {
    "host_topic_counts": {
      "reduce": "_sum",
      "map": "function (doc) {\n  emit(doc.url + '#' + doc.topic, 1);\n}"
    },
    "host_topic_triggers": {
      "map": "function (doc) {\n  emit(doc.url + '#' + doc.topic, {trigger: doc._id, username: doc.username, password: doc.password});\n}"
    },
    "all": {
      "map": "function (doc) {\n  emit(doc._id, doc.url + '#' + doc.topic);\n}"
    },
    "host_triggers": {
      "map": "function (doc) {\n  emit(doc.url, {trigger: doc._id, username: doc.username, password: doc.password});\n}"
    }
  }
}

deploy service provider

Docker is used to build and deploy the service provider.

$ docker build -t user_name/mqtt_feed_provider .
$ docker run -e CLOUDANT_USERNAME='username' -e CLOUDANT_PASSWORD='password' -p 3000:3000 user_name/mqtt_feed_provider

This image can be deployed to IBM Containers service. The application is configured to register any bound service credentials for Cloudant, rather than having to set environment variables manually.

$ docker tag user_name/mqtt_feed_provider registry.ng.bluemix.net/user_name/mqtt_feed_provider
$ docker push registry.ng.bluemix.net/user_name/mqtt_feed_provider
$ cf ic run -p 3000 -e "CCS_BIND_SRV=cloudant_instance" --name mqtt_feed_provider registry.ng.bluemix.net/user_name/mqtt_feed_provider
$ cf ic ip request
$ cf ic ip bind <ip_address> mqtt_feed_provider

Use the container's public IP address as the provider_endpoint parameter in the next section.

create package

$ wsk package create -p provider_endpoint "http://<ip_address>:3000/mqtt" mqtt

register feed action

$ wsk action create mqtt/mqtt_feed feed_action.js -a feed true

About

MQTT feed for OpenWhisk

License:MIT License


Languages

Language:JavaScript 99.2%Language:Shell 0.8%