Answer the question
In order to leave comments, you need to log in
Creating Node.js and Socket.io Dialogs?
I have a simple chat with registration and authorization written in Node.js and Socket.io . At the moment, I have implemented only a general chat for all registered users. All messages and users are stored in PostgreSQL . Now I need to make private dialogs between two users.
So far I have come up with only one idea how this can be implemented. When going to a certain URL, for example: localhost:8080/im${id} and sending a message, a dialogue is created in the database in the "dialogues" table between two users, where the first interlocutor, the second interlocutor and the ID of the last message in this dialog are passed :
INSERT INTO "public"."dialogues"("first_user", "second_user", "last_message") VALUES('3', '5', '24') RETURNING "id", "first_user", "second_user", "last_message";
Answer the question
In order to leave comments, you need to log in
socket.io has rooms/namespaces . Can't they be used?
Or something like this (from the same page)
Client
socket.on('connect', function () {
socket.emit('join', {email: [email protected]});
});
io.on('connection', function (socket) {
socket.on('join', function (data) {
socket.join(data.email);
});
});
io.to('[email protected]').emit('message', {msg: 'hello world.'});
socket.on("message", function(data) {
alert(data.msg);
});
You can store an array of {roomid, talkwith} objects in the connected user's socket object.
roomid stores the room identifier in the database, talkwith - a reference to the object of the interlocutor's socket.
When creating a room or sending a message to a specific chat, the interlocutor socket in the array of listened chats also needs to register a pair of {roomid, talkwith}, where talkwith will be a link to the dialogue initiator socket
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question