Answer the question
In order to leave comments, you need to log in
How to make private chat only between two nodejs users?
How to make private chat only between two nodejs users? if (to understand the situation) when one is online, then the other can start a chat with him and this chat is private only between them. and so with other users, i.e. like faceboock, vk. Specifically, the question is how to do this, what should I pass to the socket on the server and what code. because I only found info on how to create a room, etc., that's not it! from what it is, this is a working server with a client that connects and sends messages to each other without any kind of
client:
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io('http://messenger:8888');
$('#go').click(function(e){
e.preventDefault(); // prevents page reloading
socket.emit('chat message', $('#message').val());
$('#message').val('');
return false;
});
socket.on('chat message', function(msg){
$('.wrapper_messenger').append($('<li>').text(msg));
});
});
</script>
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
io.on('connection', function(socket){
console.log('an user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(8888, function(){
console.log('listening on *:8888');
});
Answer the question
In order to leave comments, you need to log in
1 to 1 communication in socket io can be implemented by creating a room for 2 of them, read the documentation, everything is there
Here is some good info about sockets, chats, load balancing. And yes, rooms are not used here at all. https://m.habr.com/ru/post/440546/
From myself I will add that rooms are purely virtual space. This is an abstraction over a normal connection. You can write your own system of rooms if you want. You can intelligently throw people out of rooms or vice versa create new ones. What's the question? Why are the rooms "not that"?
Rather, the question here is how to distribute the load if you have the same number of users online as on Facebook.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question