How to use Subscription

Jun 3, 2015 |

As described in the previous  section of this guide, Service Objects are the virtual representation of a physical object (e.g. sensors about temperature, GPS, accelerometer…), and they exist in the COMPOSE platform (both on servIoTicy and glue.things platform).

Every time one of the the physical object produces a new reading, it results in a sensor update being sent to the COMPOSE platform, and it is digested and processed by the Service Object associated to the physical object. In this virtual representation, each sensor is mapped to a stream, and each sensor update contains a set of values, known as channels in COMPOSE. So a Service Object can be seen as a virtual entity which has different streams (as many as sensors) and each stream produces updates composed of a tuple of channels. Each service objects deployed in the COMPOSE platform is internally stored (as virtual object on glue.things platform) and described as a JSON document (on servIoTicy platform) which contains all the necessary information to provide the data processing logic encapsulated in the service object.

The sources of data for the data processing logic can be Sensor Updates generated by a service object, as well as the result of queries for data stored in the back-end. The communication between the different service objects in the data pipeline is driven by events, and the connections between them to create data paths is built through the use of subscriptions. Service objects are deployed into the COMPOSE platform and later on accessed using a RESTful API.

In brief Data subscriptions are a mechanism in servIoTicy that allow Service Objects, Data Processing Pipelines and external data consumers to get data updates automatically and asynchronously forwarded for further processing.

In detail, the creation of subscriptions is at the core of the COMPOSE platform and allows for the definition of data processing paths that are followed by service updated being generated by device and afterwards ingested by the platform. Subscriptions can be internal, when one service object wants to get service updates generated by other service objects to be forwarded to it, or external, when entities outside of the COMPOSE platform wants to be notified about any service updates produced by one particular service object.

In order to give you an example, a client can subscribe to a data stream and receive all the updates being send to a Service Object stream. Note that given that the platform provides eventual consistency, this is a good way to get most recent updates for a Service Object stream asynchronously and in real-time. Polling is not recommended. The API operations are described here.

The following code explain the subscription usage related to the main example in the “Connect your device” tutorial.

var http = require('http');
var SERVIOTICY_HOST = "api.servioticy.com";
var SERVIOTICY_PORT = 9090;

var subscribe = function(access_token, deviceid, streamid, data){
 // Configure HTTP Request headers
 var headers = {
 'Authorization': access_token,
 'Content-Type' : 'application/json',
 'charset' : 'UTF-8'
 };

 // Configure HTTP Request options
 var options = {
 'host': SERVIOTICY_HOST,
 'port': SERVIOTICY_PORT,
 'path': "/"+deviceid+"/streams/"+streamid+"/subscriptions/",
 'method': 'POST',
 'headers': headers
 };

 // Define HTTP Request callback
 var http_req = http.request(options,
 function(res) {
 res.setEncoding('utf8');
 console.log(res);
 res.on('data', function (response) {
 console.log(response);
 });
 });

 // Set HTTP Request body
 http_req.write(data);

 // Minimal error management
 http_req.on('error', function(e) {
 console.error(e);
 });

 // Perform HTTP Request
 http_req.end();
};

var access_token = "<ACCESS_TOKEN>";// Access token associated to user
var device_id = "1444036233466662c112fd60b453a8a6888bad8f972b0";// id of the destination device
var stream_id = "sensors"; // name of the destination stream
var data= {
 "callback":"pubsub",
 "destination":"http://insert/your/destination/here"
};
data = JSON.stringify(data); // The update we want to submit

subscribe(access_token, device_id, stream_id, data);

For more specific technical info about Data subscription, check here.

Posted in: Basic tutorials, Tutorials

Comments are closed.