Skip to content

Instantly share code, notes, and snippets.

@kuzyn
Created September 13, 2016 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuzyn/9b18ac3c948c203dd25b99784cd8ea2c to your computer and use it in GitHub Desktop.
Save kuzyn/9b18ac3c948c203dd25b99784cd8ea2c to your computer and use it in GitHub Desktop.
var socket = io.connect('/');
//////////////
// Handlers //
//////////////
// fire when socket.io is connected to the back-end
socket.on('connect', function() {
getEntries(3);
});
// fire when a "newEntry" event is broadcasted
socket.on('newEntry-' + currentLocation, function(data) {
getEntries(3);
});
function getEntries(number) {
// do something client side
}
// create an entry
router.post('/', function(req, res, next) {
// our parameters
// TODO: attach client id and to entries
var payload = {
message: req.body.entryMessage,
position: {
currentState: parseInt(req.body.entryCurrentState),
currentChapter: parseInt(req.body.entryCurrentChapter)
},
location: req.body.entryLocation || 'unknown',
user: req.body.entryUser || 'unknown', //TODO: our client need to send a unique client id
timestamp: new Date()
};
// our model's method
Entry.create(payload, function(err, doc) {
if (err) {
next(helpers.generateError(req.originalUrl, 503, err));
}
res.io.emit('newEntry-' + payload.location, doc); // our socket.io event broadcast
res.io.emit('updateEntries', doc); // a general event for our admin page
res.json(doc);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment