N
N
nepster-web2015-08-16 22:30:45
JavaScript
nepster-web, 2015-08-16 22:30:45

What is the best way to organize room sharing in a chat on node.js?

It is necessary to attach a simple chat to the project, a node was chosen for this.
Once upon a time it was kolupol, but I forgot everything. In general, there are a number of authorization issues.
When a user enters the chat page, I send a special token and an identifier (interlocutor_id) of the interlocutor to the node.js server:

socket.on('connect', function () {
   socket.emit('add user', 'hash', 'interlocutor_id');
});

Next in my logic is this:
/**
     * Добавляем пользователя в комнату
     * @var hash - на основе которого необходимо проверить текущего пользователя
     * @var interlocutor_id - идентификатор собеседника
     */
    socket.on('add user', function (hash, interlocutor_id) {

        //TODO: Узнаем user_id по hash и проверяем может ли пользователь общаться в чате
        //TODO: Проверяем общий список, ожидает ли кто-то общения с user_id
        //TODO: Если кто-то ожадает общения с user_id записываем user_id в общий список и добавляем в нужную комнату
        //TODO: Если никто не ожидает общения с user_id, создаем для него комнату и вносим в общий список


        // socket.join(room);
        console.log (hash);

    });

The question itself is how to separate users into rooms so that messages do not overlap.

I'm thinking of holding something like this:

rooms = [
        {
            name: 'fds5e6f7y89sgufdgifdg5430fd',
            users : [
                {
                    soket: '1'
                },
                {
                    soket: 'interlocutor_id'
                }
            ]

        },
        {
            name: 'fdsjio87f6f7d68s7f6sdfdsf7d',
            users : [
                {
                    soket: '1'
                },
                {
                    soket: 'interlocutor_id'
                }
            ]
        }
    ];

The meaning is as follows:
I enter the chat, the chat receives data (my hash and the identifier of the interlocutor). Next, node.js checks the hash, that is, it goes to the database to see what's what, and in a good scenario gets the data of the current user.
Next, I go to my object and start searching it, hoping to find a room where there is 1 user with a suitable interlocutor_id, if the user is found (that is, someone is already waiting for me), I get the name of the room and send messages already. If the user is not found, then I generate a random name of the room and add this case to the object.
Please share your experience and comment on the approach?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mak Alexey, 2015-08-18
@S-ed

Try Socket.io:
socket.io/docs/rooms-and-namespaces/#
psitsmike.com/2011/10/node-js-and-socket-io-multir...
Ready chat:
tutorialzine.com/2014/03 /nodejs-private-webchat Tutorial
on building a chat with node.js, express, socket.io, mongo, bootstrap:
www.sitepoint.com/build-node-js-powered-chatroom-w...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question