⬆️ ⬇️

Private chat on node.js - what could be easier?

Hello friends!



It was evening, there was nothing to do ... Analyzing the example of chat in Socket.IO, I was interested in the possibility of making chat private without special tricks. The idea is not new, and simple enough - you choose your gender and age, you specify the desired gender and age of the interlocutor, and let's go! A kind of tape measure.



So let's start with the requirements:



  1. Entry form - an indication of gender and age (both for yourself and for the interlocutor);
  2. The possibility of creating private rooms (in the absence of a suitable interlocutor);
  3. Connecting to a private room (in the presence of the interlocutor);
  4. Anonymity (we will not save any user data).


')

What do we need?



Node.JS (at the time of writing - 0.6.14) with express, socket.io modules. Additionally, nib, stylus, i18n and util are used.



So let's go.



Connect new user



It's all quite simple. Create a user object, increment counters, update customer numbers.



var user = {}; var uid = socket.store.id; var room_id = uid + "_room"; online++; total++; socket.broadcast.emit('online', total, free); socket.emit('online', total, free); user.id = socket.store.id; users[socket.store.id] = user; 




User login



By input, it means already entering the chat itself - after selecting the required parameters on the initial form.

The mechanism of the interlocutor's choice is implemented in the form of a simple cycle (I do not know how fast it will work with a large load). We check the entered parameters, compare them and in case of coincidence we initiate a connection to another user, or leave it to wait.



  socket.on('nickname', function (nick, m_sex, m_age, c_sex, c_age, fn) { if (nicknames[socket.user_id]) { fn(true); } else { fn(false); nicknames[socket.store.id] = socket.nickname = socket.store.id; socket.user_id = socket.store.id; socket.gender = m_sex; var user = users[socket.store.id]; user.m_sex = m_sex; user.m_age = m_age; user.c_sex = c_sex; user.c_age = c_age; user.active = 0; free++; // Check existing rooms for(var __room in rooms){ if(user.active == 0){ _room = rooms[__room]; if(_room.c_sex == m_sex && _room.c_age == m_age && _room.m_sex == c_sex && _room.m_age == c_age && _room.active == 0){ room_id = _room.id; _room.active = 1; _room.nicknames[socket.user_id] = socket.user_id; user.active = 1 free--; console.log(' room finded! this - ' + room_id); } } } // Create own room if(user.active == 0){ console.log(' no room finded :( create own - ' + room_id); var room = {}; room.id = room_id; room.m_sex = m_sex; room.m_age = m_age; room.c_sex = c_sex; room.c_age = c_age; room.active = 0 room.nicknames = {}; room.nicknames[socket.user_id] = socket.user_id; rooms[room_id] = room; socket.emit("user message", "System", i18n.__("waiting")); } users[socket.store.id] = user; socket.join(room_id); socket.broadcast.to(room_id).emit('announcement', socket.user_id + ' ' + i18n.__('connected')); socket.broadcast.to(room_id).emit('nicknames', rooms[room_id].nicknames); socket.emit('nicknames', rooms[room_id].nicknames); } }); 




It remains the case for small - actually allow users to communicate with each other.



Sending messages



 socket.on('user message', function (msg) { msg = sanitize(msg).entityEncode(); console.log(' message from ' + socket.user_id + ' to room ' + room_id + ": " + msg); socket.broadcast.to(room_id).emit('user message', socket.gender == 1 ? i18n.__("he") : i18n.__("she") , msg); }); 




Well, actually ...



Output



 socket.on('disconnect', function () { online--; total--; socket.broadcast.emit('online', total, free); if (!socket.user_id) return; if(free > 0) free--; socket.broadcast.emit('online', total, free); console.log(socket.user_id); socket.leave(room_id); socket.broadcast.to(room_id).emit('announcement', socket.user_id + ' ' + i18n.__('disconnected')); if(rooms[room_id]) delete rooms[room_id].nicknames[socket.user_id]; if(rooms[room_id]) socket.broadcast.to(room_id).emit('nicknames', rooms[room_id].nicknames); delete users[socket.user_id]; delete nicknames[socket.user_id]; delete rooms[room_id]; }); 




That's all. It was tested under load <10 people, there were no problems :) If the community is interested in the project of such a chat - today I will post it on github, it was done just for fun.



PS The topic began to be written a long time ago, and moved from drafts - so the versions of these products may not be relevant.



PSS You can watch live here - virt.in



UPD. Promised results lie here - github.com/eudj1n/virtin

Source: https://habr.com/ru/post/141013/



All Articles