Context Services – Set a Watch for New Context

You can set a watch to get notified when new context is collected by the service. For example, if your app operates on check-ins, you can get notified when new check-ins take place. That way, your applications are sure to have the latest information.

This article tells you how to do the following:

Prerequisites

Before you begin:

Note: In the following example code, BaseURL is https://api.intel.com. Authorization is "Bearer" plus the access token granted to the application; this information is required in the header for all Cloud Services calls.
 

Set a Watch for a Specific Context Type

Post a watch to get notified when a user makes new location visit.

Location visits have this identifier: "urn:x-intel:context:type:location:checkin:v1"

function SetWatch(AccessToken) {
    jQuery.support.cors = true;
    var URL = BaseURL + '/context/v1/watches';
    var PostData = '{"data": {"contextType":
        "urn:x-intel:context:type:location:checkin:v1" }}';
    var authorization = 'Bearer ' + AccessToken;
    $.ajax({ url: URL,
        type: "POST",
        data: PostData,
        beforeSend: function (request) {
            request.setRequestHeader("authorization", authorization);
        },
        success: function (data, textStatus, jqxhr) {
            alert(jqxhr.responseText);
        },
        error: function (jqxhr, textStatus, errorThrown) {
            alert("An error occurred: " + jqxhr.status + " " + 
               textStatus + " " + errorThrown);
            alert(jqxhr.responseText);
        },
    });
}     

The following is the response to the above /watches post:

{
    "data": {
        "contextType": "urn:x-intel:context:type:location:checkin",
        "id": "afa172ce-642a-11e2-8b98-0e5a902e7b12"
    }
}

ID is the ID that's applied to the watch you just set. You can use the watch ID to find out if new context associated with that watch has been added to the service, or optionally, to delete the watch.

Upload Context to Context Services (optional)

This step is included for demonstration only. Context would normally be collected based on user actions, such as POI searches or check-ins.

Post a new item, so you can test the watch you set.

This example uploads a POI check-in item to the Context Services. The item includes elements from the POI check-in schema, including a comment and a poiRef object reference to Location Based Services.

function Upload() {
    jQuery.support.cors = true;
    var URL = BaseURL + '/context/v1/items';
    var d = new Date();
    var str = d.toISOString();
    var stx  = str.substr(0, 18) + "Z"; // truncate the date to the format 
          //specified by the JSON schema
    var PostData = '{"data": {"items": [{"contextType": 
            "urn:x-intel:context:type:location:checkin:v1",
        "clientCreatedTime": "2012-04-12T20:20:50Z","value": {"poiRef": {"domain": 
        "urn:x-intel:context:v1:object-domain:csp-poi","href": 
        "https://api.intel.com/context/v1/pois/50b845644fbf946e28000000"},
        "comment": "Yummy Pizza!"}}]}}';
    var authorization = 'Bearer ' + AccessToken;
    $.ajax({ url: URL,
        type: "POST",
        data: PostData,
        beforeSend: function (request) {
            request.setRequestHeader("authorization", authorization);
        },
        success: function (data, textStatus, jqxhr) {
            alert(jqxhr.responseText);
        },
        error: function (jqxhr, textStatus, errorThrown) {
            alert("An error occurred: " + jqxhr.status + " " + 
                textStatus + " " + errorThrown);
            alert(jqxhr.responseText);
        },
     });
}      

View the Watch Results

Call /watches/newitems to see new context posted since the last call to /watches/newitems.

function NewItems() {
    jQuery.support.cors = true;
    var URL = BaseURL + '/context/v1/watches/newitems';
    var authorization = 'Bearer ' + AccessToken;
    $.ajax({ url: URL,
        type: "GET",
        beforeSend: function (request) {
            request.setRequestHeader("authorization", authorization);
        },
        success: function (data, textStatus, jqxhr) {
            alert(jqxhr.responseText);
        },
        error: function (jqxhr, textStatus, errorThrown) {
            alert("An error occurred: " + jqxhr.status + " " + textStatus 
                + " " + errorThrown);
            alert(jqxhr.responseText);
        },
    });
}
[/javascript]The JSON response looks like this:[javascript]{
    "data": {
        "items": [
        {
            "value": {
                "comment": "Yummy Pizza!",
                "poiRef": {
                    "domain": "urn:x-intel:context:v1:object-domain:csp-poi",
                    "href":  
               "https://api.intel.com/context/v1/pois/50b845644fbf946e28000000"
                }
            },
            "clientCreatedTime": "2012-04-12T20:20:50Z",
            "contextType": "urn:x-intel:context:type:location:checkin:v1",
            "serverCreatedTime": "2013-01-22T19:55:55Z",
            "provider": "02c381a8-f424-4e15-841e-36c132d8a7dc",
            "owner": "e7464252-6f91-4884-aa7e-5b1cacf04bc3",
            "serverModifiedTime": "2013-01-22T19:55:55Z",
            "id": "bbf4410a-64cd-11e2-93a7-000000000000"
        }
    ]
  }
}        

serverCreatedTime reflects the time that a new item is posted.

Get Automatic Notifications

To automatically generate a notification whenever a new item is added that matches your watch, you can include a callbackUrl while creating the watch. The callbackUrl is a URL that is publicly addressable. If you do not include the callbackURL, you have to manually poll for newly added context. When context matching a watch with a callbackUrl is uploaded to Context Services, the user is notified with an HTTP post to the user-defined callbackUrl. For more information, see the Context Services Developer's Guide.

More Information

Context Services REST Developer's Guide

Context Services REST API Reference

Use the Behavior Model to Forecast User Behavior

Working with Context Types

Query and Paginate Context Items

Context Services Interactive Demo