An Introduction to is a javascript library used for bi-directional communication between web clients and servers. Just like Node.js, it's event based and easy to implement. uses websockets (and occasionally AJAX polling), to provide real time updates to web apps. It's perfect for things like chat rooms and twitter feeds, or anything that benefits from server side 'push notifications'. The following tutorial is a basic implementation of the library.

A Brief History

Before jumping into the, it's important to understand why is used today and how it came to be. Before websockets, developers relied heavily on AJAX polling to achieve real time update like effects. While asynchronous HTTP requests allowed for dynamic updates, it proved inefficient for several reasons. First, making continuous HTTP requests caused latency issues, especially with slower connections. Second, requests were still being made even if nothing changed on the server side. This made for am inefficient use of AJAX requests, as clients were polling regardless of an actual change in state.

Websockets to the Rescue

Then came along websockets. Websockets use a single TCP connection to leave an open bridge between clients and servers. This drastically reduces the overhead experienced with traditional HTTP requests, greatly reducing latency. Additionally, the server can push notifications to the client so updates are delivered as they happen (versus a continuous solicitation from the client). greatly simplifies the usage of websockets. It also incorporates some AJAX polling when necessary, as not all browsers support websockets, etc. has both a client side and server side library. It's extremely easy to get started. The following is a quick example of how to get started using

Configuring Your Node.js App

To include in your Node.js project, simply run:

npm install --save

This will install the module and save it to your dependencies (via your package.json file. The next step is to include the library in your application's main configuration file, typically an 'index.js' or 'server.js' file:

var app = require('express');
var http = require('http').Server(app);
var io = require('')(http);
app.get('/', function(req,res){
http.listen(5000, function(){
console.log('listening on port 5000');


This tutorial uses Express and EJS templates, however feel free to use the web framework / template engine of your choice. Note that you must bind the io module to an http server instance.

Configuring the Client

Now it's time to include the client library in your view. Include the following script tag in your HTML code, just before the ending </body> tag.

<script src='/'>
var socket = io();

This does a few things. It includes the client side library with the script tag and initializes our client

Bringing it All Together: Bi-Directional Communication

Now that we've successfully set up both the client and server library, it's time to see the true value brings to the table. In your Node.js configuration file, add the following code block:

io.on('connection', function(socket){
socket.on('clientTalk', function(){
console.log('client saying hi');
io.emit('serverTalk', 'serverData');

This defines a listener functon for the 'connection' event. The inner workings of this function will become more apparent later on. Jump back over to your client side HTML and modify the previously added script as follows:

<script src='/'>
var socket = io();
function socketMagic(){
socket.on('serverTalk', function(data){

Our socketMagic function emits an event clientTalk to the server. We've also added an event listener function for receiving the serverTalk event, which we defined in our server configuration file.

The final step is to add a client side action that will fire our newly defined socketMagic function. Below we've added a basic HTML button, which binds the onClick event to the socketMagic function.

<button onclick='socketMagic()'>Click me</button>
<div id='incoming'>

Notice that we've also added the empty div for appending messages received from our server.

If you've done everything right, you should see 'serverData' being appended every time you click the 'Click me' button.


Although this is a primitive example, it clearly demonstrates the bi-directional communication that occurs with Notice how the client is appending data directly from the server (in this case just a string 'serverData') without any HTTP request or AJAX.

Your thoughts?