node.jsexpresssocket.iochatroom

socket.io random chatroom one to one


so there is my code i am trying to create chatroom with Socket.io with rooms (one-to-one random chatroom) but i think there is bug because when i connect first two sockets it works perfectly but when i connect third socket it does not work anymore i think it is because of "numb++" but i don't know how to correct it because i have not many experience in rooms. i wanna implement : chat between 2 socket and when third client joins i wanna create second room and wait for forth socket to join to chat and etc. , and u i know i wanna one to one random chatroom like Omegle.

 var numb = 1;
 var chnm = io.of('/STchat')

 app.get('/STchat', function(req, res){

res.sendFile('C:\\Users\\tuna\\Desktop\\JUSTFOL\\views\\Tchat.html')

chnm.once('connect', function(socket){
  socket.join("room" + numb)
  console.log('made socket connection', socket.id);
chnm.in("room" + numb).clients(function(err, clients){
      if(clients.length > 2) {

     numb++
      }
});
socket.on('chat', function(data) {
 chnm.in("room" + numb).emit('chat',data)

  })
  socket.on('disconnect', function(){
    console.log('socket disconnected')
  });

}
});
app.get('/StvChat', function(req ,res){
res.sendFile(__dirname + '/views/VideoC.html');


});









var socket = io.connect('http://localhost:5000/STchat')

var message = document.getElementById('message');
handle = document.getElementById('handle');
btn = document.getElementById('send');
output = document.getElementById('output');
typing = document.createElement('p');
typing.className = "tycl";
input = document.getElementById("message");

$('form').submit(function(){


    socket.emit('chat',{
        message: message.value,

    });
    $('#message').val('');
    return false;
});
input.addEventListener("keyup", function(event) {
       event.preventDefault();

    if (event.keyCode === 13) {

      document.getElementById("send").click();
    }
  });

socket.on('chat', function(data){
  output.innerHTML += '<p>' +'  '+ data.message +'</p>' 
});

Solution

  • This was too long to fit in the comments so I have added as an answer. To clarify, here is an issue I am seeing in the way you are emitting the chat event. Lets walk through a scenario.

    var numb = 1;
    

    Two users (User A, and User B) connect to the socket and are put into room "room1". Now, numb should be incremented by your code and now numb will be == 2.

    Now, User A (who is in "room1") emits a "chat" event. Here is the issue I see:

    socket.on('chat', function(data) {
        chnm.in("room" + numb).emit('chat', data)
    
    })
    

    User A's chat event is emitted to "room2" but User A is in "room1". This happens because numb == 2 at this point in the scenario and you are using numb to emit that event, basically this is what happens chnm.in("room" + 2). User B does not get User A's emit, because it was sent to the wrong room (room2). If I misunderstood what you are trying to do please let me know.

    EDIT: As requested here is some modifications to your code that I have tested locally with success. Two users per each room.

    var numb = 1;
    var chnm = io.of('/STchat');
    
    app.get('/STchat', function(req, res) {
    
        res.sendFile('C:\\Users\\tuna\\Desktop\\JUSTFOL\\views\\Tchat.html');
    
        chnm.once('connect', function(socket) {
    
            // store the room in a var for convenience
            var room = "room" + numb;
    
            // join socket to the next open room
            socket.join(room);
    
            // store room on socket obj for use later - IMPORTANT
            socket.current_room = room;
    
            // log some stuff for testing
            console.log('made socket connection', socket.id);
            console.log("Joined room: ", socket.current_room);
    
            // Check room occupancy, increment if 2 or more
            chnm.in(room).clients(function(err, clients) {
                if (clients.length >= 2) {
                    numb++;
                }
            });
    
            socket.on('chat', function(data) {
                // emit to that sockets room, now we use that current_room
                chnm.in(socket.current_room).emit('chat', data);
            });
    
            socket.on('disconnect', function() {
                console.log('socket disconnected');
            });
        });
    
    });
    
    app.get('/StvChat', function(req, res) {
        res.sendFile(__dirname + '/views/VideoC.html');
    });