I have made a very casual commenting system, and now I want to add replies. So, when someone posts a reply on someone else's comment, that user must be notified that someone replied to their comment. In order to do that, when the replier clicks the reply button an AJAX post request is made to the server, the server then needs to get the id of the first commenter and send them a response text using socket.io (socket.io is not required to be used if there is another way to send the reply text with another module or express itself). This is my code so far:
app.post('/reply', function(req, res){
var commenterId = req.body.userId; // this is the id of the original commenter, not the replier
User.findOne({'_id':commenterId}, function(err, user){
user.send({'replied': req.user._id}); // this is what I need to do, and
//I don't know if this specific code works and that's why I'm asking if there is a way to do it with socket.io,
// io.to(socketId).emit('reply', 'some reply text here...'); // but if I do this I don't know how to get the socketId!
//Is there even a way to do this? Maybe with another module,
//or some express function I don't know about? And if it is done with express how would
//the client side code, look like? Thank you!
});
res.end();
});
commenterIdand values of the socket.io connection. you make/update this when you create the reply-able data.commenterIdis. you need to put the object value as the actual socket.io client connection, via which you can simplysend()an update notification. if this is long-term (multi reloads), then you'll need some sort of sign-in ID to key the socket.io connection by instead of the (likely) transient express one. the end result should be code usable something likeactiveUsers[commenterId].send({some:"thing"})commenterIdis the express id of the commenter. I thought that using that, I can send him, somehow, a message using express. Is there any function that allows me to send a message to him (the commenter), when the replierPOSTs his reply?