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.
This JSON message shows the replayId field in the event
object for a PushTopic event.
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.
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.
Table 1. Replay Options
| 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.
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.
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.
- 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).
After calling the setReplay() function on the extension,
the events that the subscriber receives depend on the replay value parameter passed to setReplay().