Skip to content

Instantly share code, notes, and snippets.

@julien51 julien51/push.js

Last active Dec 18, 2015
Embed
What would you like to do?
var request = require('request');
// app below needs to be a Connect/Express app.
// Subscribing: use https://push.superfeedr.com/ to use Superfeedr (works with any feed!)
function subscribe(url, conf, cb) {
var hub = conf.hub;
var params = {
method: 'POST',
uri: hub,
auth: {
'user': 'username',
'pass': 'password',
}, // Only required if you use Superfeedr as your default hub
headers: {
accept: 'application/json'
}, // On required if you use Superfeedr as your default hub and if you want JSON normalization
form: conf.form || {}
}
params.form['hub.mode'] = 'subscribe';
params.form['hub.topic'] = url;
params.form['hub.verify'] = 'sync'; // Only required if you use Superfeedr as your default hub
params.form['hub.callback'] = base + '/push/' + encodeURIComponent(url);
request(params, function(error, response, body) {
if(response.statusCode === 204) {
console.log('Subscribed to', url);
cb();
}
else if(response.statusCode === 204) {
console.log('Will verify intent subscription to', url);
cb();
}
else {
console.error(response.statusCode)
console.error(body);
}
})
}
// Verification of intent. Not required when using https://push.superfeedr.com/
// You probably want to customiaz this to echo the challenge *only if* you really want to subscribe to the
// request.query['hub.topic']. Also, keep track of request.query['hub.lease_seconds'] because it indicates when/if
// you subscription will expire.
app.get('/push/:feed', function(request, response) {
response.send(request.query['hub.challenge']);
});
//
app.post('/push/:feed', function(request, response) {
// Do something with the body (needs to get it raw)
response.send('Thx');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.