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

Connecting to Multiple PushTopics #48

Closed fdidron closed 10 years ago

fdidron commented 10 years ago

Hi Kevin,

First let me thank you for the great wrapper, It works very well and it's very straightforward to use. I've been trying to connect to two different PushTopics in my app, but it seems I am getting only message from the last topic I connected. Any way to make this working ?

Thanks !

Here is my implementation

var sfdc = nforce.createConnection({
  clientId: config.SF.CLIENT_ID,
  clientSecret: config.SF.CLIENT_SECRET,
  redirectUri: config.SF.REDIRECT_URI,
  apiVersion: config.SF.API_VERSION,
  environment: config.SF.ENV,
  mode: 'single'
});

var authSFDC = function(callback){

    sfdc.authenticate({ username: config.SF.USERNAME, password: config.SF.PASSWORD }, function(err, resp){
        if(!err){
            logClient.captureMessage('Salesforce HandShake Successfull', {level: 'info'});
            callback(resp);
        }
        else logClient.captureMessage('Salesforce HandShake Failed', {level: 'error', extra: {'error': err}});
    });

};

var streamHandler = function (stream,table,callback){

    str.on('connect', function(){
        logClient.captureMessage('Connected to ' + table.pushTopic + ' PushTopic', {level: 'info'});
    });

    str.on('error', function(err) {
        logClient.captureMessage('Connecting to ' + table.pushTopic + ' PushTopic Failed', {level: 'error', extra: {'error': err}});
        console.log(err);
    });

    str.on('data', function(data) {
        callback(data);
    });

};

authSFDC(function(oauth){

        var tables = config.DB_MAPPING;

        for( i=0; i < tables.length; i++){
            var table = tables[i];
// Looped twice with two different pushTopics
jobsStream = sfdc.stream({ topic: table.pushTopic, oauth: oauth });
                streamHandler(jobsStream, table, function(data){
                    logClient.captureMessage(table.table + ' data ' + data.event.type, {level: 'info', extra: {'data': data}});
                });
        }

    });
fdidron commented 10 years ago

My bad, wrong coding from my side, it works :)

kevinohara80 commented 10 years ago

Haha, no worries!

fdidron commented 10 years ago

Hi Kevin,

I have one question though, I let my node app online for 4 days now and it seems the subscription stream didn't disconnect / break which is great. Is the subscription persistent ? Is it possible to listen to an event when it disconnects (transport:down) ?

Kind regards,

Florian

On Mon, Mar 3, 2014 at 6:06 AM, kevinohara80 notifications@github.comwrote:

Haha, no worries!

Reply to this email directly or view it on GitHubhttps://github.com/kevinohara80/nforce/issues/48#issuecomment-36466831 .

kevinohara80 commented 10 years ago

@111studio Sorry, just saw this. You might want to look through the Faye library. That's what nforce uses for the streaming. The returned stream object probably emits many more events and I bet you can find them in the Faye docs.