Simple Socket.io Example

If you're ever working with real-time javascript, try Socket.io.

<!DOCTYPE html>
<html>
  <head>
    <title>My App</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="Chris Mendez">
    <link rel="shortcut icon" href="/icons/logo-256x256.ico" type="image/vnd.microsoft.icon">
    <!--Bootstrap-->
    <link rel="stylesheet" href="/css/plugins/bootstrap.min.css">
    <!--Bootstrap Theme-->
    <link rel="stylesheet" href="/css/themes/bootstrap.min.css">
    <!--Bootstrap Responsive-->
    <link rel="stylesheet" href="/css/plugins/bootstrap-responsive.min.css">
    <!--jQuery-->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <!--Google Web Fonts-->
    <script src="//ajax.googleapis.com/ajax/libs/webfont/1.1.2/webfont.js"></script>
    <!--Twitter Bootstrap-->
    <script src="/js/plugins/bootstrap.min.js"></script>
    <!--Socket.io-->
    <script src="/socket.io/socket.io.js"></script>
    <!--Global-->
    <script>
      $(document).ready(function() {
      	var socket = io.connect('http://localhost');
	    	//Listen for server handler "news"
	    	trace( "Listening for 'news' event sent from server.js" );
	    	socket.on('news', function ( data ){
		    	trace( "'news' event fired:: " + data )
			    trace( "sending data.hello to 'tmpEvent' w/ message " + data.hello );
			    socket.emit('tmpEvent', { my: data.hello });
		});

      });
    </script>
  </head>
  <body>
  	<header>

		</header>

		<section>
			<p></p>
		</section>
    <footer></footer>  
  </body>
</html>
/************************************************
* Title: Simple Socket.io
* Desc: A simple socket.io example
* Modified:
* Notes:
* 
************************************************/

var app = require('http').createServer(handler);
var fs = require('fs');
var io = require('socket.io').listen(app);

function handler (req, res) {
  console.log( req, res );
  fs.readFile( __dirname + '/views/index.html', function (err, data)
  {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }
    res.writeHead(200);
    //Write out index.html
    res.end(data);
  });
}

//Sockets can be used on top of page rendering
io.sockets.on('connection', function (socket) {
  //Send data to client (index.html)
  socket.emit('news', { hello: 'world' });
  //Receive data from client (index.html)
  socket.on('tmpEvent', function (data) {
    console.log( "'tmpEvent' fired::", data.my );
  });
});

app.listen( process.env['app_port'] || 8080 );