Posted on by & filed under Content - Highlights and Reviews, Programming & Development, Web Development.

As more and more applications are made available as web applications, there will be an increased need to provide real-time synchronization and collaboration facilities in these applications. Providing fault-tolerant communication along with a scalable server backbone with a web app is no small feat. It is even harder since web apps, and specifically JavaScript, are not designed to handle complex tasks. Fortunately, we have two great tools available to solve exactly these kind of complex problems: Node.js and

On the server-side we have Node.js, a powerful, open source stack that enables server code to be written in JavaScript. Node is built on an asynchronous, event-based server with the ability to easily handle multiple clients simultaneously. It is an ideal platform for writing the scalable server-side backbone of real-time applications. More information about Node can be found at

On top of Node and client-side JavaScript is a real-time communication framework available by the name of Socket abstracts the various transport methods available for communication between the server and client. It automatically handles things like handshake, heartbeats, and timeouts and exposes a clean and simple interface to developers. Read more about in Professional Node.js: Building Javascript Based Scalable Software. Socket is production ready and is being used in many high traffic real-time web apps. The official website is available at

To set up the development environment, make sure that Node.js is installed. Once done, type the following in the console:

This will install the server-side module for using the package manager of Node. Next, create app.js and type in the following:

Then create another file, index.html, and type in the following:

Then open the console and type in the following to start the Node server:

Navigate your browser to http://localhost:8000/. A blank page will be opened and the browser will prompt you to enter your name. Type it in and then open the console where you started the server. You should see a line ‘Hello, …. ‘ where the server is greeting you.

Let’s quickly walk through the code to see what is happening. In the app.js file, most of the code is standard Node functionality, except for a few new things. We use the module by requiring it and assigning it to the variable io. We defined a callback handler for handling http connections and it basically just returns the index.html file in the present directory. The interesting part starts from the line io.sockets.on(‘connection’, …). What is basically happening is that we are binding the “connection” event to a callback (which we are defining inline). The connection event is fired whenever a client is connected to the server. We can use this event to bind to other events of the client. This is exactly what we have done. We further bind to the ‘client-event’ where we output the name to the console. We have also called the emit function to trigger the event ‘server-event’ which is handled by the JavaScript on the client side as explained below.

In the index.html file, we connect to the server at localhost and attach a callback to the server-event event. The callback prompts the user to enter her name and then emits the client-event and also passes the name as optional data with the event. The server then uses this data to output the name on the console.

This is just the tip of the iceberg. The combination of Node.js and enables developers to write highly scalable, real-time web applications with ease. Take a look at this chapter in Smashing Node.js: JavaScript Everywhere, 2nd Edition for more in depth information about using with Node.js.

Safari Books Online has the content you need

Check out these Node.js books available from Safari Books Online:

In just 24 sessions of one hour or less, Sams Teach Yourself Node.js in 24 Hours will help you master the Node.js platform and use it to build server-side applications with extraordinary speed and scalability. Using this text’s straightforward, step-by-step approach, you’ll move from basic installation, configuration, and programming all the way through real-time messaging between browser and server, testing and deployment. Every lesson and case-study application builds on what you’ve already learned, giving you a rock-solid foundation for real-world success!
Node.js is a powerful and popular new framework for writing scalable network programs using JavaScript. Professional Node.js begins with an overview of Node.js and then quickly dives into the code, core concepts, and APIs. In-depth coverage pares down the essentials to cover debugging, unit testing, and flow control so that you can start building and testing your own modules right away.
This book shows you how to transfer your JavaScript skills to server side programming. With simple examples and supporting code, Node Cookbook talks you through various server side scenarios often saving you time, effort, and trouble by demonstrating best practices and showing you how to avoid security faux pas.
Take your web development skills from browser to server with Node—and learn how to write fast, highly scalable network applications on this JavaScript-based platform. With Learning Node, you’ll quickly master Node’s core fundamentals, gain experience with several built-in and contributed modules, and learn the differences and parallels between client- and server-side programming.
Node Web Development gives you an excellent starting point straight into the heart of developing server side web applications with node. You will learn, through practical examples, how to use the HTTP Server and Client objects, the Connect and Express application frameworks, the algorithms for asynchronous execution, and use both SQL and MongoDB databases.

About the author

Shaneeb Kamran is a Computer Engineer from one of the leading universities of Pakistan. His programming journey started at the age of 12 and ever since he has dabbled himself in every new and shiny software technology he could get his hands on. He is currently involved in a startup that is working on cloud computing products.

Tags: handshake, heartbeats, Javascript, Node.js,, timeouts, web apps,

Comments are closed.