title: PHP minimal working example of Web Sockets
body: <p>I'm trying to determine how to setup a web socket for the first time ever so a working minimal example with static variables (IP address for example instead of <code>getservbyname</code>) will help me understand what is flowing where.</p>
  
  <p>I want to do this the right way so no frameworks or addons for <em>both</em> the client and the server. I want to use PHP's native web sockets as described here though without over-complicating things with in-depth classes...</p>
  
  <p><a href="http://www.php.net/manual/en/intro.sockets.php" rel="nofollow">http://www.php.net/manual/en/intro.sockets.php</a></p>
  
  <p>I've already put together some basic JavaScript...</p>
  
  <pre><code>window.onload = function(e)
  {
   if ('WebSocket' in window)
   {
    var socket = new WebSocket('ws://'+path.split('http://')[1]+'mail/');
  
    socket.onopen = function () {alert('Web Socket: connected.');}
  
    socket.onmessage = function (event) {alert('Web Socket: '+event.data);} 
   }
  }
  </code></pre>
  
  <p>It's the PHP part that I'm not really sure about. Presuming we have a blank PHP file...</p>
  
  <ol>
  <li><p>If necessary how do I determine if my server's PHP install has this socket functionality already available?</p></li>
  <li><p>Is the request essentially handled as a <code>GET</code> or <code>POST</code> request in
  example?</p></li>
  <li><p>Do I need to worry about the port numbers? e.g. <code>if
  ($_SERVER['SERVER_PORT']=='8080')</code></p></li>
  <li><p>How do I return a basic message on the initial connection?</p></li>
  <li><p>How do I return a basic message say, five seconds later?</p></li>
  </ol>