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

1

6

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 ?

Popularly answered 23/10, 2017 at 14:8 Comment(0)
C
12

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;

}
Consolidation answered 23/10, 2017 at 14:27 Comment(10)
how can i name a chat room dynamically e.g if abc want to send message to xyz then ? ur case(conversation id )\Popularly
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.Consolidation
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 ?Popularly
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.Consolidation
ok is there way to push particular particular user to particular room in front end and backendPopularly
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.Consolidation
Let us continue this discussion in chat.Popularly
@Popularly if this helped you, please mark it as the answer.Consolidation
@Consolidation if there is 100 conversations for an user, then how can i listen to all conversations of him on the front end side?Idolize
want to clarify, so you saying that if I want only two users to connect like me and some of my friends and no one can read our message what's the app still I've to create the room but that room only will exist between those two right??Gazzo

© 2022 - 2024 — McMap. All rights reserved.