Newer Version Available

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

Message Durability

Salesforce stores events for 24 hours, so you can retrieve stored events during that retention window. The Streaming API event framework decouples event producers from event consumers. A subscriber can retrieve events at any time and isn’t restricted to listening to events at the time they’re sent.

Event Numbering

Each broadcasted event is assigned a numeric ID. IDs are incremented and not guaranteed to be contiguous for consecutive events. Each ID is guaranteed to be higher than the ID of the previous event. For example, the event following the event with ID 999 can have an ID of 1,025. The ID is unique for the org and the channel. The IDs of deleted events aren’t reused.

The ID is added in the replayId field of the notification message. For example, this JSON message shows the replayId field in the event object for a generic event.

1{
2 "clientId":"a1ps4wpe52qytvcvbsko09tapc",
3 "data":{
4    "event":{
5            "createdDate":"2016-03-29T19:05:28.334Z",
6            "replayId":55
7        },
8    "payload":"This is a message."
9    },
10 "channel":"/u/TestStreaming"
11}

This JSON message shows the replayId field in the event object for a PushTopic event.

1{
2  "clientId":"2t80j2hcog29sdh9ihjd9643a",
3  "data":{
4    "event":{
5        "createdDate":"2016-03-29T16:40:08.208Z",
6        "replayId":13,
7        "type":"created"
8     },
9     "sobject":{
10         "Website":null,
11         "Id":"001D000000KnaXjIAJ",
12         "Name":"TicTacToe"
13     }
14  },
15  "channel":"/topic/TestAccountStreaming"
16}

In API version 37.0 and later, the time format of the createdDate field value has changed to make it consistent with the time format used in the Salesforce app. The time portion now ends with a Z suffix instead of +0000. Both suffixes denote a UTC time zone.

Note

Replaying Events

A subscriber can choose which events to receive, such as all events within the retention window or starting after a particular event. The default is to receive only the new events sent after subscribing. Events outside the 24-hour retention period are discarded.

This high-level diagram shows how event consumers can read a stream of events by using various replay options.

Diagram showing a stream of events with replay options
Table 1. Replay Options
Replay Option Description
Replay ID Subscriber receives all events after the event specified by its replayId value.
-1 Subscriber receives new events that are broadcast after the client subscribes.
-2 Subscriber receives all events, including past events that are within the 24-hour retention window and new events sent after subscription.

To replay events, use the Streaming API endpoint.

1https://Salesforce_Instance/cometd/38.0/

Durable streaming is supported at this endpoint starting with API version 37.0. Durable Generic Streaming is supported in version 36.0 at this alternative endpoint: https://Salesforce_Instance/cometd/replay/36.0/. However, we recommend you upgrade to version 37.0 and use the main Streaming API endpoint.

Note

The replay mechanism is implemented in a Salesforce-provided CometD extension. A sample extension is provided in JavaScript and another in Java. For example, you can register the extension as follows in JavaScript.
1// Register streaming extension
2var replayExtension = new cometdReplayExtension();
3replayExtension.setChannel(<Streaming Channel to Subscribe to>);
4replayExtension.setReplay(<Event Replay Option>);
5cometd.registerExtension('myReplayExtensionName', replayExtension);
  • The argument passed to setReplay() is one of the replay options listed in Replay Options.
  • The first argument passed to registerExtension() is the name of the replay extension in your code. In the example, it’s set to myExtensionName, but it can be any string. You use this name to unregister the extension later on.
  • If the setReplay() function isn’t called, or the CometD extension isn’t registered, only new events are sent to the subscriber (same as the -1 option).

Note

After calling the setReplay() function on the extension, the events that the subscriber receives depend on the replay value parameter passed to setReplay().

Code Samples

Visualforce Sample
For a sample and code walkthrough that uses Visualforce and a CometD extension in JavaScript, see: Example: Replay PushTopic and Generic Streaming Events Using a Visualforce Page