0

I'm using Mongoose to store the chat between different registered users, at the current implementation they can retrieve chat logs if they refresh the page! which is not identical to be a real-time solution. How to use socket.io() to act as sender/receiver for many users.

I followed
https://socket.io/get-started/chat/ and https://medium.freecodecamp.org/simple-chat-application-in-node-js-using-express-mongoose-and-socket-io-ee62d94f5804

But both, they assume the same port for the same user.

Vikrant
  • 4,920
  • 17
  • 48
  • 72
Hasoun GH
  • 43
  • 2
  • 6

1 Answers1

0

It is always a better idea to refer to the official API documentation.

https://socket.io/docs/client-api/

In your case, you need to create a separate socket for a separate private chat

//Join PrivateRoom - client
var socket = io.connect('http://localhost');
socket.emit('joinprivate', {email: user1@example.com});

//Server
var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.on('joinprivate', function (data) {
    socket.join(data.email); // We are using room of socket io
  });
});

More examples:

How to send a message to a particular client with socket.io

Jeba Prince
  • 1,669
  • 4
  • 22
  • 41