Newer Version Available

This content describes an older version of this product. View Latest

Subscribe to Platform Event Notifications with CometD

Use CometD to subscribe to platform events in an external client.

Pub/Sub API is newer than the CometD-based Streaming API. You can use Pub/Sub API to publish and subscribe to platform events and change data capture events. Based on gRPC API and HTTP/2, Pub/Sub API efficiently publishes and delivers binary event messages and supports multiple programming languages. For more information about Pub/Sub API, see Subscribe to Platform Event Notifications with Pub/Sub API.

Important

Salesforce sends platform events to CometD clients sequentially in the order they’re received. The order of event notifications is based on the replay ID of events. A CometD client can receive a batch of events at once. The number of event messages in a batch can vary. If the client uses a buffer for the received events, ensure that the buffer size is large enough to hold all event messages in the batch. The buffer size needed depends on the publishing rate and the event message size. At a minimum, set the buffer size to 10 MB, and adjust it higher if needed.

The process of subscribing to platform event notifications through CometD is similar to subscribing to PushTopics or generic events. The only difference is the channel name. The platform event channel name is case-sensitive and is in the following format.

1/event/Event_Name__e

To subscribe to a custom channel, use this format.

1/event/Channel_Name__chn

Use this CometD endpoint with the API version appended to it.

1/cometd/66.0

Example

If you have a platform event named Low Ink, provide this channel name when subscribing.

1/event/Low_Ink__e

The message of a delivered platform event looks similar to the following example for Low Ink events.

1{
2  "data": {
3    "schema": "dffQ2QLzDNHqwB8_sHMxdA", 
4    "payload": {
5      "CreatedDate": "2017-04-09T18:31:40.517Z", 
6      "CreatedById": "005D0000001cSZs", 
7      "Printer_Model__c": "XZO-5", 
8      "Serial_Number__c": "12345", 
9      "Ink_Percentage__c": 0.2
10    }, 
11    "event": {
12      "EventUuid": "2ec0e371-1395-457f-9275-be1b527a72f7",
13      "replayId": 2
14    }
15  }, 
16  "channel": "/event/Low_Ink__e"
17}

The schema field in the event message contains the ID of the platform event schema. The schema is versioned—when the schema changes, the schema ID changes as well. For more information about retrieving the event schema, see Get the Event Schema.

Add custom logic to your client to perform some operations after a platform event notification is received. For example, the client can create a request to order a new cartridge for this printer model.