Getting started with Socket.IO and Node.js

Post to Twitter

Node.js is not the cure for everything, however, it can certainly makes working with Websockets very easy when using the library. Using websockets you can easily build realtime applications and even multi-player games. Today I’ll show you how to easily build a simple chat program using Node.js and

[ad name=”Google Adsense”]

Note: To go through this article you will need to have Node.js installed and working correctly. You will also need some sort of text editor, I’ll be using WebStorm but anything you wish to use should work. Some of the code in this article has been modified from this excellent source:

On you computer create a new folder somewhere convenient called: chat. Inside that folder you can add two files called: app.js and index.html

Let’s fill in the app.js file first.

var fs = require('fs')
    , http = require('http')
    , socketio = require('');

var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-type': 'text/html'});
    res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(8080, function() {
    console.log('Listening at: http://localhost:8080');

socketio.listen(server).on('connection', function (socket) {
    socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);

The above code is pretty bare minimum for a chat server. The server sends the index.html file and listens for any incoming websockets. If you were to send a message like “hi” the format would look something like the following:


The index.html page is also very minimal and looks like the following:

    <script src=""></script>
    <script src="/"></script>
            var iosocket = io.connect();

            iosocket.on('connect', function () {

                iosocket.on('message', function(message) {
                iosocket.on('disconnect', function() {

            $('#outgoingChatMessage').keypress(function(event) {
                if(event.which == 13) {
Incoming Chat:&nbsp;<ul id="incomingChatMessages"></ul>
<br />
<input type="text" id="outgoingChatMessage">

The webpage is not very attractive but it works. Open two browsers preferably ones that support WebSockets (Chrome or Safari will work).

The next step is to install by doing the following in a terminal (or command prompt):

$ npm install

Run the app.js file:

$ node app.js

Now with your two browsers go to the following path and try it out: http://localhost:8080/

Post to Twitter

This entry was posted in JavaScript, Node.js, Open Source. Bookmark the permalink.

5 Responses to Getting started with Socket.IO and Node.js

  1. Look my github!

    I built a simples web analytics with Socket.IO it shows on real-time what browser and SO the user is using.

    And on my blog there are many posts about Node.js I hope you enjoy it even the blog being in portuguese hehehe


  2. Pingback: Getting started with Socket.IO and Node.js | House of Ellis

  3. Pingback: Socket.IO 和 Node.js 入门 | 技术作坊

  4. Pingback: Working with SockJS, CoffeeScript and Node.js | Giant Flying Saucer

  5. Pingback: Working with JQuery, AJAX, CoffeeScript and Node.js | Giant Flying Saucer

Comments are closed.