title: Web application using websockets and node.js
body: <p>I'm new to HTML5 and node.js. I'm trying to create a very basic client-server application. Here is the code.</p>
  
  <p>Server side (node.js):</p>
  
  <pre><code>var net = require('net');
  
  var server = net.createServer(function(c) {
  
      console.log('client connected');
      c.setEncoding('utf8');
  
      c.on('end', function() {
          console.log('client disconnected');
      });
  
      c.on('data', function(data) {
          console.log(data);
          c.write("Got it");
      });
  
  });
  
  server.listen(9998);
  </code></pre>
  
  <p>Client side (websockets):</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
      &lt;head&gt;
          &lt;script&gt;
  
              try {
  
                  var ws = new WebSocket('ws://127.0.0.1:9998');
  
                  ws.onopen = function() {
                      ws.send("Message to send");
                      alert("Message is sent...");
                  };
  
                  ws.onmessage = function (evt) { 
                      var message = evt.data;
                      alert("Message is received: " + message);
                  };
  
                  ws.onclose = function() { 
                      alert("Connection is closed..."); 
                  };
  
              } catch (err) {
                  alert(err.message);
              }
  
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>As far as I understand, the client should connect to the server, send "Message to send" and the server should reply with "Got it". Instead what the server receives is an http GET request for the client html page and none of the client callbacks are ever fired. What am I missing?</p>