Sunday, 23 April 2017

How to make user NOT see own name in list of users in socket chat

I'm building a simple chat using this guide.

When someone login their name is appended to the list of online users. However, each user should not see his/her own name in the list, only the name of the other users. Any suggestions to what I should add/change in my code to fix this?

client-side:

socket.on("update-people", function(data){
      $("#people").empty();
      if (true) {}
      $('#people').append("<li class=\"list-group-item active\">People online <span class=\"badge\">"+data.count+"</span></li>");
      $.each(data.people, function(a, obj) {
        $('#people').append("<li><a href=\"#\" class=\"list-group-item\"><span>" + obj.name + "</span></a></li>");
      });
    });

server-side:

socket.on("connection", function(client) {
client.on("join", function(name){
    console.log("Someone joined the chat", name);
    roomID = null;
    people[client.id] = {"name" : name, "room" : roomID};
    sizePeople = _.size(people);
    sizeRooms = _.size(rooms);
    socket.sockets.emit("update-people", {people: people, count: sizePeople});
    socket.sockets.emit("roomList", {rooms: rooms, count: sizeRooms});
    client.emit("updateToSelf", "You have connected to the server. Join or create room to chat");
    client.broadcast.emit('updateToOthers', name + " is online.");
    clients.push(client); //populate the clients array with the client object
});



via JonasSH

No comments:

Post a Comment