Skip to content

Instantly share code, notes, and snippets.

@ReidCarlberg
Created December 19, 2013 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReidCarlberg/8045742 to your computer and use it in GitHub Desktop.
Save ReidCarlberg/8045742 to your computer and use it in GitHub Desktop.
// If you use this as a template, replace IBM Corp. with your own name.
// Sample Node-RED node file
// Require main module
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var nforce = require('nforce');
var http = require('http'),
faye = require('faye');
var client;
var oauth;
function SalesforceInNode(n) {
RED.nodes.createNode(this,n);
this.active = true;
this.topic = n.topic;
var node = this;
console.log(n);
var org = nforce.createConnection({
clientId: n.clientId,
clientSecret: n.clientSecret,
redirectUri: n.redirectUri
});
org.authenticate({ username: n.username, password: n.password + n.token}, function(err, _oauth) {
if(err) {
console.error('unable to authenticate to sfdc');
console.log(err);
process.exit(code=0);
} else {
console.log("authenticated");
console.log("oauth");
console.log(_oauth);
oauth = _oauth;
client = new faye.Client(oauth.instance_url + '/cometd/28.0');
client.setHeader("Authorization", "OAuth " + oauth.access_token);
var subscription = client.subscribe('/topic/' + n.pushTopicName, function(message) {
var msg = { topic:n.pushTopicName, payload:message.sobject, location:"Chicago", lang:"en" };
node.send(msg);
//console.log("received!");
//console.log(message.sobject);
});
}
});
this.on("close", function() {
console.log("closing");
});
}
// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("salesforce in",SalesforceInNode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment