Created
March 3, 2017 07:34
-
-
Save Calvin-Huang/12c708b59072d19c03bab8577ebf0fe7 to your computer and use it in GitHub Desktop.
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
// 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