kevinohara80 / nforce

nforce is a node.js salesforce REST API wrapper for force.com, database.com, and salesforce.com
MIT License
474 stars 167 forks source link

replay functionality is not working - Platform events #178

Open gorugontula opened 5 years ago

gorugontula commented 5 years ago

I am trying to subscribe to a platform event in salesforce and it works fine - except ability to replay events -either from a specific replayId or from the earliest available (-2). Below is the code that I am using - which is taken from a sample code provided in a salesforce doc but is not working - any help would be greatly appreciated.

`org.authenticate({ username: USERNAME, password: PASSWORD,securityToken: SECURITY_TOKEN }, function(err, oauth) { if(err) return console.log("Error authenticating to Salesforce, " + err); var client = org.createStreamClient();

        REPLAY_ID = -2;
        console.log("Latest REPLAY_ID - ", REPLAY_ID);          
      }
    });

    var accs = client.subscribe({topic:TOPIC, isEvent:true, **retry: -2**});`

I have also tried using "replayId" and "replayFrom" instead of retry but still no good. I am running this code from AWS Lambda - Node JS .

parthi22 commented 5 years ago

same issue for me as well.

kevinohara80 commented 5 years ago

It's possible that this is working now in streaming 2.0 https://github.com/kevinohara80/nforce/blob/streaming-2.0/lib/fdcstream.js

I'm hoping to have 2.0 out within a week.

Once I have a beta out, it would be great if you both would test it for me and report back?

Alasano commented 5 years ago

Not 100% related but digging through here did allow me to make my pushtopic work in 1.12.2

Connecting in single mode as described in the doc I was getting this error

"400::The replayId for channel {/topic/mypushtopic} wasn't found using the " +
    'provided replay ID map {{}}. Ensure that the channel name you provided in ' +
    'the replay map is valid and matches the channel name used for subscribing.'

I replaced the fdcstream.js in the 1.12.2 release with the one from the streaming-2.0 branch and changed the way I subscribe to the pushtopic (passing in /topic/mypushtopic instead of just the topic name, found here https://github.com/kevinohara80/nforce/blob/streaming-2.0/examples/stream.js)

With that I'm able to connect succesfully for the moment. Looking forward to the full 2.0 release, especially the typescript part! Nice work @kevinohara80

agentgill commented 5 years ago

This workaround worked for me, thanks @Alasano. thank you @kevinohara80 for providing this super simple node library.

gorugontula commented 5 years ago

I took the latest version of committed fdcstream.js and updated my local files and it looks like the replay functionality works fine now. Thanks @kevinohara80 for actively working on the library to keep it update. This has been very handy.

karoldylewski commented 5 years ago

This workaround also works for me, thanks @Alasano ! I changed fdcstream.js from: https://github.com/kevinohara80/nforce/blob/streaming-2.0/lib/fdcstream.js and changed the way i subscribe platform event (from eventName__e to /event/eventName__e)

samjeffress commented 5 years ago

Hi, is there any chance that the streaming-2.0 branch will merge to master? Anything we can do to help get it there?

I'm new to salesforce devving, and would like to subscribe to platform events, but am not so excited about copying files around. More than happy to take a fork and give it a test.

samjeffress commented 5 years ago

Alternatively, are you able to publish your streaming-2.0 library to npm as a beta?

hassanazharkhan commented 4 years ago

This workaround also works for me, thanks @Alasano ! & @karoldylewski Finally I ended up with below code and its working fine with typescript.

import * as nforce from 'nforce'
import * as config from 'config'
import oAuth from '../types/oAuth'

class SalesforcePlatformEvents {
  private organization: any;
  constructor() {
    this.organization = nforce.createConnection({
      clientId: config.get('app.nforceKeys.clientId'),
      clientSecret: config.get('app.nforceKeys.clientSecret'),
      redirectUri: config.get('app.nforceKeys.redirectUri'),
      environment: config.get('app.nforceKeys.environment'),
      apiVersion: 'v44.0',
    })
  }

  public startEventListner() {
    const self = this.organization
    self.authenticate({
      username: config.get('app.nforceCredentials.user'),
      password: config.get('app.nforceCredentials.pass'),
    },
    function(err: any, oauth: oAuth) {
      if (err) return console.log(err)
      if (!err) console.log('*** Successfully connected to Salesforce ***')

      const client = self.createStreamClient({oauth})
      const sub = client.subscribe({topic: config.get('app.nforceTopic.topic')})

      client.on('connect', function() {
        console.log('*** Streaming client transport: UP ***')
      })

      client.on('disconnect', function(data: any) {
        console.log('*** Streaming disconnect : ' + data.reason)
        console.log('*** Disconnect data', data)
      })

      sub.on('connect', function() {
        console.log('*** Connected to topic: ' + sub.getTopic())
      })

      sub.on('error', function(error: Error) {
        console.log('*** Error: ' + error)
        sub.cancel()
        client.disconnect()
      })

      sub.on('data', function(data: any) {
        console.log('Data: ', data)
        console.log('Current replay id: ' + sub.getReplayId())
      })
    })
  }
}

export default SalesforcePlatformEvents