Receiving real-time data

Real-time data is received for tags. You can construct tags either by browsing or by providing already known tag references (strings) that resolve into tags through the fromReferenceAsync method of the Tag Class.

Subscribe and Unsubscribe

UaolCloud.subscribeAsync(tags)

Subscribe to real-time data updates for the given tags. Tags can be constructed by either exploring sites and topics or by resolving tag references.

Arguments:
  • tags (Array.<Tag>) – Tags to subscribe to
Returns:

Promise – Returns a null result

UaolCloud.unsubscribeAsync(tags)

Unsubscribe from real-time data updates for the given tags.

Arguments:
  • tags (Array.<Tag>) – Tags to unsubscribe from
Returns:

Promise – Returns a null result

Examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function subscribe(tags) {
    UaolCloud.subscribeAsync(tags).then(() => {
        console.log('subscription successful...')
    })
    .catch((error) => {
        console.error(error);
    });
}

function onUpdateLiveValues(values) {
    var numValues = values.length;
    for (var i=0; i<numValues; i++) {
        console.log(values[i].value);
    }
}

function onUpdateLiveValuesError(error) {
    console.log(error);
}

Error handling

Should an error be reported by the subscribeAsync function or via the onUpdateLiveValuesError callback function then the recommended action is:

  1. Stop the session (stopSession)
  2. Wait for 30 seconds
  3. Start the session (startSession)
  4. Re-create tags from references previously saved (Tag.fromReferenceAsync)
  5. Subscribe again (subscribeAsync)