Last active
April 6, 2018 11:55
-
-
Save haugstrup/7420255 to your computer and use it in GitHub Desktop.
Sample for working with Podio's CometD/Bayeux service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Based on example code from Carl-Fredrik Herö from Elvenite AB <http://elvenite.se/> | |
Install dependencies: | |
* npm install faye | |
* npm install git://github.com/haugstrup/podiojs.git | |
Add needed info: | |
* Populate client_id and client_secret for your API key | |
* Populate podioUser with your credentials | |
Start server: | |
* node push-sample.js | |
*/ | |
var http = require('http'), | |
faye = require('faye'); | |
// Initialize podiojs client | |
var podio = require('podiojs'); | |
podio.client.client_id = ''; | |
podio.client.client_secret = ''; | |
// Login details | |
var podioUser = { | |
username: '', | |
password: '', | |
user_id: | |
}; | |
// Error handling for podiojs | |
podio.on('error', function(apiRequest, response, body){ | |
console.error('Podio Error:', body); | |
}); | |
// Initialize faye client | |
var fayeClient = new faye.Client('https://push.podio.com/faye'); | |
// Extend faye client with signature and timestamp used for authentication | |
fayeClient.addExtension({ | |
'outgoing': function(message, callback) { | |
message.ext = message.ext || {}; | |
message.ext = {private_pub_signature: push.channel.signature, private_pub_timestamp: push.channel.timestamp}; | |
callback(message); | |
} | |
}); | |
// Simple push object for handling a subscription | |
var push = { | |
subscription: null, | |
channel: null, | |
messageReceived: function(message) { | |
console.log("New message received: ", message); | |
// You probably want to filter out your own messages: | |
if (message.data.created_by.type == 'user' && message.data.created_by.id != podioUser.user_id){ | |
// ... do something. | |
} | |
}, | |
addSubscription: function(channel) { | |
this.channel = channel; | |
this.subscription = fayeClient.subscribe(this.channel.channel, this.messageReceived); | |
this.subscription.then(function(){ | |
console.log('Subscription is now active'); | |
}, function(error){ | |
console.error('Subscription failed: ', error.message, error); | |
}); | |
} | |
}; | |
// Authenticate with Podio | |
// Locate push channel for user object | |
// Add subscription to channel | |
podio.authenticate('password', {'username': podioUser.username, 'password': podioUser.password}, function(response, body){ | |
podio.get('/user/status', {}, function(response, body){ | |
push.addSubscription(body.push); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this code expected to work? I can't get any of it to work. The initial initialization is wrong, podio.on() isn't even a function of the podio.api.
Or it just outdated?