title: How to receive web socket message from server in client side by html5 socket api
body: <p>from couple of days i'm fighting with web sockets. But, yet now i couldn't able to get grip on it. </p>
  
  <p>I manage to build a server side file by PHP which is sending message to a particular address+port. But, when i'm trying to access that message in client side by HTML5 socket api, i can't able to get that. </p>
  
  <p>Here is my server side PHP cile code..</p>
  
  <pre><code> $address = "127.0.0.1";
   $service_port = 80;
  
  error_reporting(E_ALL);
  
  /* Create a TCP/IP socket. */
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  if ($socket === false) {
      echo "socket_create() failed: reason: " . 
      socket_strerror(socket_last_error()) . "\n";
  }
  
  echo "Attempting to connect to '$address' on port '$service_port'...";
  $result = socket_connect($socket, $address, $service_port);
  if ($result === false) {
     echo "socket_connect() failed.\nReason: ($result) " . 
        socket_strerror(socket_last_error($socket)) . "\n";
  }
  
  $in = "HEAD / HTTP/1.1\r\n";
  $in .= "Host: www.google.com\r\n";
  $in .= "Connection: Close\r\n\r\n";
  $out = '';
  
  echo "Sending HTTP HEAD request...";
  if(!socket_write($socket, $in, strlen($in)))
  {
      $errorcode = socket_last_error();
      $errormsg = socket_strerror($errorcode);
  
      die("Could not send message on socket : [$errorcode] $errormsg \n");
  }
  echo "OK.\n";
  </code></pre>
  
  <p>In my client side i'm using this following code.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;    
  // Let us open a web socket
  var ws = new WebSocket("ws://127.0.0.1:80/");
  ws.onopen = function()
  {
      // Web Socket is connected, send data using send()
      ws.send("Message to send");
      alert("Message is sent...");
  };
  ws.onmessage = function (evt) 
  { 
      var received_msg = evt.data;
      alert("Message is received...");
  };
  ws.onclose = function()
  { 
      // websocket is closed.
      alert("Connection is closed..."); 
  };  
  &lt;/script&gt;
  
  echo "Reading response:\n\n";
  while ($out = socket_read($socket, 2048)) {
      echo "&lt;br&gt;&lt;br&gt;$out&lt;br&gt;&lt;br&gt;";
  }
  
  socket_close($socket);    
  </code></pre>
  
  <p>But, i can't able to receive the server side message by using this javascript. </p>
  
  <p>This is the first time i'm trying to put my hand in web sockets. SO, i've totally no idea how to do this. I also search a lot in google but can't able to figure out where &amp; what i'm doing wrong.</p>
  
  <p>Regards</p>