better approach for one to one and group chat nodejs Socket.io

13,061

One-to-one and group chat is very similar. If we use rooms in socket.io https://socket.io/docs/rooms-and-namespaces/#.

In one-to-one only 2 users are in the same room. where as in a group chat more than 2 people can be in the same room.

So when a person sends a mesage, they send it to the room they belong to and want to chat with instead of sending it to individual users. Then, everyone in that room, whether its one other person or multiple people will receive it.

- Node.js

Join a client to a room

io.on('connection', function(client){
     client.join(conversation._id)
});

Emitting a message to a room

   client.broadcast.to(conversation_id).emit('message:send:response', {
       msg: res.msg,
       conversation_id: res.data._id
  });

- Angular

Listen to emitted messages

getMessages(conversation_id) {

    let observable = new Observable(observer => {

        this.socket.on('message:send:response', (chat) => {

            if (chat.conversation_id === conversation_id) {
                observer.next(chat.msg);
            }
        })

    });

    return observable;

}
Share:
13,061
Asad
Author by

Asad

I did graduation from University of engineering and technology,Lahore. I am full stack developer. currently I am working as front-end developer.

Updated on June 10, 2022

Comments

  • Asad
    Asad almost 2 years

    I am implementing one to one and group chat in my application in NodeJs using socket.io and angular 4 on client side. I am new to socket.io and angular 4.I want to ask what is better approach for this e.g if user want to send a message to specific user or it want to send a message to group of users.

    According to my Rnd should I keep the obj of all connected users and that obj contain the socket id of that user with his user name (email or what ever ) so that if some user want to send message to some one we should need his user name and we can access his id through his user name . Or is there any solution except this ? And where should i keep that obj of user and socket id global variable or database ?

  • Asad
    Asad over 6 years
    how can i name a chat room dynamically e.g if abc want to send message to xyz then ? ur case(conversation id )\
  • Kay
    Kay over 6 years
    Look at the documentation my friend. socket.io/docs/rooms-and-namespaces/#. Under joining and leaving. socket.join('some room'); so in your case it would be socket.join('xyz'); socket is your user which is created on connection.
  • Asad
    Asad over 6 years
    I have seen this docs but could not understand if I write code in nodejs like this io.on('connection', function(socket){ socket.join('some room'); }); i think every user will go to this room . am right ?
  • Kay
    Kay over 6 years
    yes ur right every user will go to this room. Thats obviously not what you want. So must create a function to joinRoom(socket, id). So in this function only the socket passed in should join the id of the room.
  • Asad
    Asad over 6 years
    ok is there way to push particular particular user to particular room in front end and backend
  • Kay
    Kay over 6 years
    my recommendation to you, is to attempt bit by bit and you should create a new question with specific problems you face, rather than using this question for everything.
  • Asad
    Asad over 6 years
  • Kay
    Kay about 3 years
    @Asad if this helped you, please mark it as the answer.