Newer Version Available
Replay Events Sample: Code Walkthrough
JavaScript Sample for Replaying Events
The sample contains JavaScript code that uses the cometdReplayExtension replay extension to replay events. This code is in the StreamingGenericMessageDisplay Visualforce component, which is embedded in a Visualforce page. You can add the JavaScript directly to your Visualforce page or reuse the Visualforce component. Portions of the sample are highlighted in this section.
The first step is to register the Salesforce-provided CometD extension cometdReplayExtension to replay events. This snippet also sets the streaming channel and the replay option. The first argument in registerExtension is an arbitrary name, which you use to unregister the extension.
1// Register Generic Streaming Replay extension
2var replayExtension = new cometdReplayExtension();
3replayExtension.setChannel(<Streaming Channel to Subscribe to>);
4replayExtension.setReplay(<Event Replay Option>);
5cometd.registerExtension('myReplayExtensionName', replayExtension);Next, the client connects to the CometD replay endpoint. The API version in the endpoint must be 36.0 or later. The session ID value of the current session is passed in the Authorization header.
1// Connect to the CometD endpoint
2cometd.init({
3 url: 'https://Salesforce_instance/cometd/replay/36.0/',
4 requestHeaders: { Authorization: 'OAuth <Session ID>'}
5});The last step is to specify a callback for the CometD subscribe() function. CometD calls this callback function when a message is received in the channel. In this sample, the callback function displays the message data to the page. It appends the data to the div HTML element whose ID value is "content".
1// Subscribe to a topic. JSON-encoded update will be returned in the callback
2cometd.subscribe(channel, function(message) {
3 $('#content').append('<p>Notification ' +
4
'on channel: ' + JSON.stringify(message.channel) + '<br>' +
5
'Payload: ' + JSON.stringify(message.data.payload) + '<br>' +
6
'Replay Id: ' + JSON.stringify(message.data.event.replayId) + '<br>' +
7
'Full message: ' + JSON.stringify(message) + '</p>');
8
}
9);cometdReplayExtension Extension
The cometdReplayExtension provides callbacks that are called for incoming and outgoing messages. These callbacks implement the logic that checks for the extension’s registration on handshake and sets the replay option on subscription.
On handshake, the callback for incoming messages checks whether the replay extension has been registered. If so, it sets the _extensionEnabled variable to true.
1this.incoming = function(message) {
2 if (message.channel === '/meta/handshake') {
3 if (message.ext && message.ext[REPLAY_FROM_KEY] == true) {
4 _extensionEnabled = true;
5 }
6 }
7}On subscription, the callback for outgoing messages checks whether the replay extension has been registered by inspecting the _extensionEnabled variable. If the extension is registered, the callback subscribes to events based on the specified replay option. The sample sets the replay option by calling the extension’s setReplay() function.
1this.outgoing = function(message) {
2 if (message.channel === '/meta/subscribe') {
3 if (_extensionEnabled) {
4 if (!message.ext) {
5 message.ext = {};
6 }
7 var replayFromMap = {};
8 replayFromMap[_channel] = _replay;
9 // add "ext : { "replay" : { CHANNEL : REPLAY_VALUE }}"
10 // to subscribe message.
11 message.ext[REPLAY_FROM_KEY] = replayFromMap;
12 }
13 }
14};