Skip to content

Instantly share code, notes, and snippets.

@haugstrup
Last active April 6, 2018 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save haugstrup/7420255 to your computer and use it in GitHub Desktop.
Save haugstrup/7420255 to your computer and use it in GitHub Desktop.
Sample for working with Podio's CometD/Bayeux service.
/*
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);
});
});
@adf1969
Copy link

adf1969 commented Nov 29, 2017

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?

@apnikhil
Copy link

apnikhil commented Apr 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment