title: Websockets: send messages and notifications to all clients except sender
body: <p>I am developing chat based on websockets and webrtc. I would like to send messages to all connected users except sender but I cannot find suitable solution. To be more specific, I would like to send notifications to other  connected users that new user has joined to the chat.
  I am trying to give a unique ID to every connected user, but the first assigned ID is re-writed by every new user and I cannot diferentiate users.</p>
  
  <p>Server:</p>
  
  <pre><code>// list of users
  var CLIENTS=[];
  var id;
  
  // web server is using 8081 port
  var webSocketServer = new WebSocketServer.Server({ port: 8081 });
  
  // check if connection is established
  webSocketServer.on('connection', function(ws) {
  
  id = Math.random();
  CLIENTS[id] = ws;
  CLIENTS.push(ws);
  
  ws.on('message', function(message) {
      console.log('received: %s', message);
      var received = JSON.parse(message);
  
      if(received.type == "login"){
          ws.send(message);  // send message to itself
  
          /* *********************************************************** */
          /* *** Here I trying to check if message comes from sender *** */
  
          sendNotes(JSON.stringify({
              user: received.name,
              type: "notes"
          }), ws, id);
  
          /* *********************************************************** */
      }else if(received.type == "message"){
          sendAll(message); // broadcast messages to everyone including sender
      }
  
  });
  
  ws.on('close', function() {
      console.log('user ' + CLIENTS[ws] + ' left chat');
      delete CLIENTS[ws];
  });
  
  });
  
  function sendNotes(message, ws, id) {
      console.log('sendNotes : ', id);
      if (CLIENTS[id] !== ws) {
          console.log('IF : ', message);
          for (var i = 0; i &lt; CLIENTS.length; i++) {
              CLIENTS[i].send(message);
          }
      }else{
          console.log('ELSE : ', message);
      }
  }
  
     function sendAll(message) {
         for (var i=0; i &lt; CLIENTS.length; i++) {
            CLIENTS[i].send(message); // broadcast messages to everyone including sender
         }
     }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>loginButton.addEventListener("click", function(){
      name = usernameInput.value;
  
      if(name.length &gt; 0){
          socket.send(JSON.stringify({
              type: "login",
              name: name
          }));
      }
  
  });
  
  function sendData() {
      var data = dataChannelSend.value;
      var userName = document.getElementById('greetingUser').innerHTML;
  
      socket.send(JSON.stringify({
          username : userName,  // fetch user name from browser, after login
          type : "message",
          message : data
      }));
  }
  
  socket.onmessage = function(message) {
  
      var envelope = JSON.parse(message.data);
      switch(envelope.type) {
          case "login":
              onLogin(envelope);
              break;
          case "message":
              showMessage(envelope);
              break;
      }
  };
  </code></pre>
  
  <p>I would highly appreciate If you could give me any hint. Thanks</p>