jsforce

Salesforce event subscription using JSForce


I am trying to subscribe to events generated by the salesforce topic. I am able to receive events, my node application is not able to read events that are in the queue, "generated before my node application is up and running". It's able to receive events that are generated once the node js application is up.

My code looks like this

var jsforce = require('jsforce');
var conn = new jsforce.Connection(loginUrl:sfHost);
conn.login(userId, pwd, function(err: userInfo){
  if(err){return console.log(err); }
  console.log("Connected");
  conn.streaming.topic("eventName").subscribe(function(message){
    console.log(message);
  }
}

The above code able to get data generated after the node application is up and running, It's not able to read data whatever in the queue. which are generated earlier and not read using any application.

Thanks for your help and time on this.


Solution

  • There is a process to read old events, using reply id...

    var jsforce = require('jsforce');
    var conn = new jsforce.Connection(loginUrl:sfHost);
    
    conn.login(userId, pwd, function(err: userInfo) {
        const replayId = -2; // -2 is all retained events
        const replayExt = new jsforce.StreamingExtension.Replay(channel, replayId);
        const fayeClient = conn.streaming.createClient([ replayExt ]);
    
        if(err) { return console.log(err); }
    
        console.log("Connected");
    
        const subscription = fayeClient.subscribe(channel, data => {
            console.log('topic received data', data);
        });
    }
    

    for more info please look at https://github.com/jsforce/jsforce/blob/master/build/jsforce-api-streaming.js

    enter image description here

    https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/using_streaming_api_durability.htm