user3137766
user3137766

Reputation:

Handle chat system with socket.io, nodejs and mongodb

I have to create a chat system for our client online shop, we use nodejs/mongodb/socket.io.

I just tested if the realtime conversation between my nodejs and a simple html page is working :

const server = app.listen(app.get('port'), () => {
    console.log("working");

});

var io = require('socket.io').listen(server);
io.on('connection', function(socket){

    socket.on('chat message', function(msg){
    console.log('message: ' + msg);

});

    socket.emit('I just emit here for test', 'hello');
});

This code just working, now the fun part is to be able to make a shop manager discuss to a client, but clients shouldn't able to discuss between them (I have already a "clients" and "shop manager" collection ) like below picture :

chat system with socket.io

So, could someone tells me what is the best workflow to achieve this?, it means how to identify a client? and begin conversation to him? i am a bit lost with socket.io ...

Thanks for your help.

Upvotes: 1

Views: 1610

Answers (2)

Tejashwi Kalp Taru
Tejashwi Kalp Taru

Reputation: 3074

  1. Create a message schema which will store conversations between client and manager.
  2. Create a schema which will hold rooms or communication IDs.

Whenever a client logs into your system, show him the list of rooms, and when a client picks any room, show him the conversation.

Now, how to create a room? Whenever a client wants to communicate with a manager (let's use ClientA and ManagerA), check if the conversation already exists or not. If a communication already exists, create a unique room like "clientA-room-managerA" and add both the parties to that room, store all the conversations in existing messages schema.

If the communication does not exist, create a room like "clientA-room-managerA" and then create a chat record in the schema which will hold communication Ids, and then start the room same as before "clientA-room-managerA"

By this way you should be able to create multiple chat records for a client and manager.

A client will not be able to communicate with other clients

A Manager can have communication with various clients

Consider a manager with user id: 123456789 and client with 0987654321, then you can create a room like:

var room = manager.user_id + "-room-" + client.user_id;
// room = 123456789-room-0987654321
//then join the client and manager to the room
manager_socket.join(room);
client_socket.join(room);

//you can send a message in a room:
io.sockets.in(room).emit('new_message',{"msg":"hi there"});

PS: This is a kind of group chat where the group contains 2 members i.e. Client and a manager. For reference you can see this links:

http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/ https://github.com/jgonera/socket.io-multichat

Upvotes: 1

jacobedawson
jacobedawson

Reputation: 3212

In socket.io each socket has an individual ID which you can use to communicate between particular sockets. You can assign a particular value to the ID if you want, for example the user's MongoDB _id, for example:

this.userEmit(userID).emit('got user settings', settings);
// where userEmit is a custom function that will handle an error case when the id can't be found

You can also broadcast to specific "rooms", whereby only sockets that are members of that room will receive the message, e.g.:

        io.sockets.in(groupID).emit('update group status', {
            groupID: groupID,
            onlineMembers: x.length
        });

See their documentation for some examples: https://socket.io/docs/rooms-and-namespaces/

Upvotes: 0

Related Questions