title: What is the best way to map websockets connection to users in the database?
body: <p>I am looking to map websockets connection with users in the database and have something like this at the moment:</p>
  
  <pre><code>function connectToNotifServer(){
  
  var conn = new WebSocket('ws://localhost:8080');
  
  conn.onopen = function(e) {
      alert("Connection established!");
      conn.send(JSON.stringify({user_id: sessionStorage.getItem("user_id")}));
  };
  
  conn.onmessage = function(e) {
      alert(e.data);
  };
  
  conn.onclose = function(e) {
      alert("Connection closed!");
  };
  return conn;
  
  }
  </code></pre>
  
  <p>I then have a PHP websockets server, that keeps a mapping of a connection to user_id.</p>
  
  <p>However, I think there is a security flaw, as a user could simply inject any value he want in the user_id and impersonate another user. Is there a better way to keep this mapping, but without sending the user_id or sending it in another way?</p>
  
  <p>Thanks  </p>