Jim
Jim

Reputation: 141

Node.js: Using Socket.io in an express.js route to send message to a specific client

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();
});

Upvotes: 0

Views: 1112

Answers (1)

user6361120
user6361120

Reputation:

//app.js file   
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var routes = require('./routes/routes')(io);
app.use('/', routes);

//router file
var express = require('express');
var router = express.Router();
var _socket = null;

//list of socket users.once they logout delete the socket by 
//delete users[_socket.userid];
var users = {};

var returnRouter = function(io) {

    io.sockets.on('connection', function (socket) {
        //now _Socket is available inside routes
       _socket =  socket;
    });

    router.post('/login', function(req, res) {
        //authentication logic
        User.findOne({'email': req.body.email}, function (err, user) {

           //userid must be unique
           _socket.userId= user.userId
           //set session variable to store id of the user
           req.session.userId = user.userId;
           //now every user has a socket associated with their id
           users[_socket.userId] = _socket;
        });
    });

    router.post('/reply', function (req, res) {
       var commenterId = req.body.userId;
       User.findOne({'_id': commenterId}, function (err, user) {

       // you can get the id of the logged in user that is the creator
       //of the original post from req.session.userId
       //if you have implemented session store

       //the commenter user object is obtained from findOne method
       users[req.session.userId].emit('notification', {
         notification: user.username+' commented on your post'
       }});
       });
       res.end();
    });

    return router;
};
module.exports = returnRouter;

Upvotes: 1

Related Questions