I am trying to create an application of the type messenger. I connect to the database using node.js mysql. Each person who logs on the website is initially connected to a room for example: "room juan" (only John connects to this room) said room I use to update the list of friends of this user
socket.join("room juan"); //cada usuario tiene su propio room y nadie más ingresa //en el
socket.join("juan");
when a user loguea appears with the status "connected" in the database. (The records that have repeated the "room juan" is because they are friends of Juan)
users_online
room | nickname | status
-------------------------------------------
room juan | juan | **desconectado**
-------------------------------------------
room juan | maria | desconectado
-------------------------------------------
room ana | dario | desconectado
Juan logueo, then set his status to connected
setStatusToConnected(juan);
function setStatusToConnected("juan")
{
con.query(
'UPDATE users_online SET status = "connected" WHERE nickname = ?', ["juan"]);
}
The database updates and Juan's status is now connected
users_online
room | nickname | status
-------------------------------------------
room juan | juan | **conectado**
-------------------------------------------
room juan | maria | desconectado
-------------------------------------------
room ana | dario | desconectado
I update users online, in this array friend_list = [];
store users with status connected
updateUsersOnline(room juan, juan);
function updateUsersOnline(room juan,juan) {
friend_list = [];
con.query(
'SELECT * FROM users_online WHERE status="connected"',
[room],
function(err,rows){
if(err) throw err;
for (var i in rows) {
var obj = { room: rows[i].room, nickname: rows[i].nickname};
friend_list.push(obj);
io.sockets.in(rows[i].room).emit('view_data', friend_list,rows[i].room);
}
}
);
I issue an event for each room that has the recently connected user, that is, if several users are connected to the room of Juan I issue an event for those rooms that have the user in question and I update the list that shows the users online
io.sockets.in(rows[i].room).emit('view_data', friend_list,rows[i].room);
CLIENTE
socket.on('view_data', function (friends,room){
for(var i=0; i<friends.length; i++) {
if(friends[i].room==room){
console.log(friendList[i].nickname);
}
}
My question is, this way of showing the friends of the user that loguea in the page, is a good practice ?, is well ?, in the long run can lead to problems ?, is the only way I came up with to update and be able to save the contacts of the users. Then I would like to store the messages of the users when they communicate with each other, for example the table would be of this style:
tabla_mensajes
origen | destinatario | mensaje
-------------------------------------------
maria | juan | hola juan como estas?
-------------------------------------------
juan | maria | muy bien y vos?
-------------------------------------------
maria | juan | tambien :)
and when the user clicks on the friend who wants to converse on the client, I create an event that communicates with the server requesting the old messages such as:
'SELECT mensaje FROM tabla_mensajes WHERE origen=? AND destinatario=?'
(Or something like that), what do you think? Am I going the right way or not?