Node.js Chat Application – Part 1

I’m writing this tutorial as I’m learning, so this tutorial is intended for beginners.
I know there are tons of node.js tutorials out there; However, I’ve always wanted to write a tutorial of my own, plus I really liked the outcome of the chat application I wrote, therefore I have decided to publish this tutorial.

The complete code can be found here.
The code for this part can be found here.
The code for each part of the tutorial can be found under the ‘tutorials’ folder.

I hope you’ll enjoy and like this tutorial.
Either way please leave a comment! 🙂

What is Node.js

As I mentioned previously, there are tons of tutorials out there and I guess most of them explain what node.js is, or how to install it, so I won’t get in to it.
Installing and running node.js is really simple and there is a small ‘hello world’ example on http://nodejs.org.

Node.js® is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications.
Node.js uses an event-driven, non-blocking I/O model that makes it
lightweight and efficient, perfect for data-intensive real-time
applications that run across distributed devices.

http://nodejs.org

Setting up the server

First thing I did was to setup a package.json file. Doing this makes the installation of server dependencies (such as Socket.io & Express) a bit easier.

{
    "name": "simple-chat-with-rooms-example",
    "version": "0.0.1",
    "description": "Simple Multi-Rooms Chat Application",
    "dependencies": {}
}

Next thing we do is create a server.js file.
This JavaScript file will act as our ‘main’.

var
express		= require('express'),
app			= express(),
http		= require('http').Server(app);

/* Send response to client */
app.get('/', function(req, res) {
	res.send('wow! such server! very listening!');
});

/* Start listening on port 4040 */
http.listen(4040, function(){
	console.log('Chat server listening on *:4040');
});

Let’s test our server

Before we can run the server, we’ll have to install the server dependencies.
At this point the only dependency module we require is ‘Express’ (http://expressjs.com/).
Express is a minimal node.js web application, and it will help us serve files such as index.html, chat.css & chat.js.

To install Express execute the following command:

$ npm install --save express

Once the above command is executed, you will notice that your package.json file has updated, and now it includes the Express module dependency. In addition the folder ‘node_modules’ was created in your root directory.

The last thing we need to do now is to run our server.
So once again, execute the following:

$ node server.js

Node.js should print out ‘Chat server listening on *:4040’ which means our server is up and running. Now fire up your browser, go to http://127.0.0.1:4040 and prepare to be amazed! 🙂

That’s all for this time.
On the next part of the tutorial I’ll explain how to make our server serve files such as html, js & css.
I’ll also introduce Socket.io and show a basic example of client-server communication using Socket.io.

I hope you enjoyed reading this part as much as I did writing it.
If you have any questions or comments feel free to leave a reply.
Till next time…

3 thoughts on “Node.js Chat Application – Part 1

Leave a comment