8. Delta Publish and Subscribe

Introduction

Delta messaging in AMPS has two independent aspects:

  • delta subscribe allows subscribers to receive just the fields that are updated within a message.
  • delta publish allows publishers to update and add fields within a message by publishing only the updates into the SOW.

This chapter describes how to create delta publish and delta subscribe commands using the AMPS JavaScript client. For a discussion of this capability, how it works, and how message types support this capability see the AMPS User Guide.

Delta Subscribe

To delta subscribe, you simply use the delta_subscribe command as follows:

// assumes that client is connected

client.execute(

    // The delta_subscribe command to execute
    new amps.Command('delta_subscribe')
        .topic('delta-topic')
        .filter('/thingIWant = "true"'),

    // Message handler
    function(message) { /* Delta messages arrive here */ }

);

Example 8.1: Using delta_subscribe Command

The convenience method Client.deltaSubscribe() is available. If the delta_subscribe is not a valid command for the topic provided, a regular subscription will be created.

client.deltaSubscribe(
    function(deltaMessage) { ... },  // Message handler
    'delta-topic',                   // Topic
    '/thingIWant = "true"'           // Filter (optional)
)

// Once subscribed, it resolves with the subscription id.
.then(function(subId) { ... });

Example 8.2: Using deltaSubscribe()

As described in the AMPS User Guide, messages provided to a delta subscription will contain the fields used to generate the SOW key and any changed fields in the message. Your application is responsible for choosing how to handle the changed fields.

Delta Publish

To delta publish, you use the Client.deltaPublish() method as follows:

// assumes that client is connected

var message = ... ;  // obtain changed fields here

client.deltaPublish('my-topic', message);

Example 8.3: Using deltaPublish()

The message that you provide to AMPS must include the fields that the topic uses to generate the SOW key. Otherwise, AMPS will not be able to identify the message to update. For SOW topics that use a User-Generated SOW Key, use the Command form of delta_publish to set the SowKey.

// assumes that client is connected

var message = ... ;  // obtain changed fields here
var key = ... ;      // obtain user-generated SOW key

var command = new amps.Command('delta_publish');
command.topic('delta-topic');
command.sowKey(key);
command.data(message);

/**
* Execute the delta publish. Do not provide a message
* handler since delta_publish is not waiting for the
* acknowledgment from the server.
*/
client.execute(command);

Example 8.4: Using delta_publish command for topics with user-generated SOW keys