When Codey Outfitters runs its Bayeux client, the client subscribes to Streaming API on the products publication channel.
Codey Outfitters uses a Visualforce page to show its customers updates to prices and inventory and new and discontinued outdoor gear. This sample source code for the Visualforce page is on Github, and you can modify it to work with your OData 4.0 producer.
The heart of the subscription mechanism is in the _subscribeChannels method in src/js/channel/ServerConnection.js.
cometd.subscribe(channelName, function(event) {
if (event && event.data && event.data.payload &&
event.data.payload.ChangeEventHeader) {
var header = event.data.payload.ChangeEventHeader;
var entityName = header.entityName;
var recordId = header.recordIds[0];
var timestamp = header.commitTimestamp;
var changeType = header.changeType;
switch (changeType) {
case "UPDATE":
// Update logic ...
break;
case "CREATE":
// Create logic
break;
case "DELETE":
// Delete logic
break;
}
}
}
The channelName for Codey Outfitters’ products is /data/Products__ChangeEvent. Each change event notification that’s published on the channel is in the Bayeux format and represents an updated record in the product inventory. The data.payload contains the change event data and includes a header that describes the nature of the change event. This sample event notification describes a change to a Codey Outfitters product record.
{
schema=”lswW55LyUCYeU7raSrmZ5A”,
payload={
ChangeEventHeader={
commitUser=”005xx000001SvAn”,
sequenceNumber=15,
entityName=”Products__x”,
changeType=”UPDATE”,
changeOrigin=”Products?$deltaToken=3779”,
transactionKey=”f158f145-a...”,
commitTimestamp=1507759119826,
recordIds=[“x03xx0000000008”]
},
Name__c=”Camping Stove”,
ExternalId=7801,
UnitPrice__c=10.0,
Stock__c=1500.0,
OrderLimit__c=12.0,
ProductId__c=7801.0
},
event={replayId=15}
}
Each change event notification has this structure.
| Field | Description |
|---|---|
| schema | Versioned schema reference that defines the change event. |
| payload |
Details about the change event.
|
| event | The replay ID that refers to the position of the event notification in the event stream on a channel. |
Each change event header contains these fields.
| Field | Description |
|---|---|
| commitUser | User who publishes the event. |
| sequenceNumber | Change event sequence number. Each change occurs in the order in which it’s received. |
| entityName | API name of the external object. |
| changeType | Specifies whether a record was updated, created, or deleted. |
| changeOrigin | Delta link that’s used to request the change. The OData producer calculates the delta token while executing the previous change request. |
| transactionKey | Unique identifier for the transaction. |
| commitTimestamp | Specifies when the change event notification is published in milliseconds since January 1, 1970. |
| recordIds | Salesforce ID of the external object record that’s updated, created, or deleted. To see the current values of the record’s fields, query the external object using this record ID. |
| nulledFields | List of fields set to null. |