Skip to content

Instantly share code, notes, and snippets.

@Calvin-Huang
Created March 3, 2017 07:34
Show Gist options
  • Save Calvin-Huang/12c708b59072d19c03bab8577ebf0fe7 to your computer and use it in GitHub Desktop.
Save Calvin-Huang/12c708b59072d19c03bab8577ebf0fe7 to your computer and use it in GitHub Desktop.
// Verify with FB console.
app.get('/fb-subscribe', (req, res, next) => {
if (req.param('hub.mode') == 'subscribe' && req.param('hub.verify_token') == process.env.VERIFY_TOKEN) {
res.send(req.param('hub.challenge'));
} else {
res.sendStatus(400);
}
});
// Subscriber for receiving FB notification.
app.post('/fb-subscribe',
// Middleware for authorization.
(req, res, next) => {
if (req.isXHub && req.isXHubValid()) {
return next();
} else {
res.sendStatus(401);
}
},
(req, res) => {
const { entry, object } = req.body;
if (object === 'page') {
entry.forEach((eachEntry) => {
const { changes } = eachEntry;
changes.forEach((change) => {
const { field, value: { sender_name, sender_id, item, post_id, verb, message } } = change;
// Handle feed
if (field === 'feed') {
// Add new comment into redis and broadcast to subscribers.
if (item === 'comment' && verb === 'add') {
const videoId = post_id.split('_')[1];
const redis = createClient(redisUrl);
redis.rpush(`live:${videoId}:comments`, message);
redis.publish(`live:${videoId}:comments:latest`, message);
redis.quit();
}
}
});
});
}
res.send('OK');
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment