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. Implement your own CometD client or use EMP Connector, an open-source, community-supported tool that implements all the details of connecting to CometD and listening on a channel.

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

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

1/cometd/54.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.

To determine if the schema of an event has changed, retrieve the schema through REST API. Use the schema ID by performing a GET request to this REST API resource: /services/data/vXX.X/event/eventSchema/schemaId. Alternatively, you can retrieve the event schema by supplying the event name to this endpoint: /services/data/vXX.X/sobjects/eventName/eventSchema. For more information, see:

You can use EMP Connector to receive delivered events. The connector subscribes to any type of streaming event and accepts the event channel name as an argument. See Example: Subscribe to and Replay Events Using a Java Client (EMP Connector).

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.