0

I'm currently trying to get my application running on Heroku to listen for events from the Streaming API, but have had no success. The application works fine when I'm testing it locally against my org Sandbox, but when I deploy the application to Heroku and access my Production org, I don't receive any events.

I've tested the Streaming API on Production with the developer workbench and it received the events from the API without issue. It only appears to have issues on the Heroku server.

I'm using the node.js and JSForce library to connect to Salesforce and subscribe to the desired topic.

Here is my simplified code:

 var conn = new jsforce.Connection({
  oauth2: {
    clientId: config.salesforce.clientId,
    clientSecret: config.salesforce.clientSecret,
    redirectUri: config.salesforce.callbackURL
  },
  accessToken: account.salesforce.accessToken.access_token,
  instanceUrl: account.salesforce.accessToken.instance_url,
  refreshToken: account.salesforce.refreshToken
});

conn.on('refresh', function(accessToken, result) {
  // store new access token
});

conn.streaming.topic('TOPIC_NAME').subscribe(function(message) {
  console.log(message);
});

The application connects to Salesforce without issue and I'm able to make all other API calls without issue, it only seems to not work with the streaming API.

No errors are generated in the Heroku log either, so I'm not sure how to proceed with troubleshooting.

Has anyone else had issues with Heroku and the Streaming API? Any suggestions on how I can resolve this?

0

The issue was resolved by creating a new JSForce Connection object after the access token was successfully refreshed and subscribing to that new connection:

var conn = new jsforce.Connection({
  oauth2: {
    clientId: config.salesforce.clientId,
    clientSecret: config.salesforce.clientSecret,
    redirectUri: config.salesforce.callbackURL
  },
  accessToken: account.salesforce.accessToken.access_token,
  instanceUrl: account.salesforce.accessToken.instance_url,
  refreshToken: account.salesforce.refreshToken
});

var connectToStream = function(connection) {
  connection.streaming.topic('TOPIC_NAME').subscribe(function(message) {
    console.log(message);
  });
};
connectToStream(conn);

conn.on('refresh', function(accessToken, result) {
  // store new access token
  var newConnection = new jsforce.Connection({
    accessToken: accessToken,
    instanceUrl: result.instance_url
  });
  connectToStream(newConnection);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.