title: Creating a chat with nodejs ws (einaros)
body: <p>Yesterday I started using this module. I'm trying to make a chat for do something with websockets.</p>
  
  <p>I have the problem, that I don't know how to pass the message that sends a client to the server for then show it for all users. In the server side I have this code:</p>
  
  <pre><code>var ipaddress = 'localhost';
  var port = 8080;
  
  var WebSocketServer = require('ws').Server
      , ws = new WebSocketServer({host:ipaddress, port:port});
  
  ws.on('connection', function(ws) {
      console.log('New connection');
      ws.on('message', function(message) {
          ws.send(message);
      });
  });
  
  console.log('Listening to ' + ipaddress + ':' + port + ' ...');
  </code></pre>
  
  <p>And in the client side this other code:</p>
  
  <pre><code>jQuery(document).ready(function($) {
      var content = $('#screen');
      var message = $('input[type="text"]').val();
      var ws = new WebSocket('ws://localhost:8080');
  
      ws.onopen = function() {
          console.log('Connected');
      };
  
      $('input[type="button"]').click(function() {
          ws.send(message);
      });
  
      ws.onmessage = function(msg) {
          console.log('Received message from server: ' + msg.data);
          addMessage(msg.data);
      }
  
      function addMessage(message) {
          content.prepend('&lt;p&gt;&lt;span&gt;' + message + '&lt;/span&gt;&lt;/p&gt;');
      }
  });
  </code></pre>