Tuesday 23 May 2017

Socket.io rooms is sending to everybody, not just single room

I have no idea why this won't work; here's my code:

      io.on('connection', function(socket) {
        socket.on('join', function(data) {
          socket.join(data.email); // We are using room of socket io
            User.findById(bid.highestBidder, (err, theUser) => {
              io.sockets.in(theUser.email).emit('outbid', {msg: 'You have been outbid!'});
            });
        });
      });

Now, here's the code on the client-side javascript:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();

  var userEmail = $("#userEmail").val();

  socket.emit('join', {
    email: userEmail
  });

  socket.on("outbid", function(data) {
        console.log(data);
      });
</script>

If you need me to paste more code, let me know and I will.

Basically, when the socket function gets executed, it's sending it to every browser instead of just the room with the "email."



via kolbykskk

No comments:

Post a Comment