Building a Real-time Two-player "Words in a Word" game with Node.js and Socket.io

LinksLive Demo, Github Repository
Over the weekend I was working on Node.js and socket.io. I managed to develop boilerplate game app. Its a two-player word-game in which both players have to find words from a given word. The scores are calculated by finding the length of the words. The player with the maximum score wins. Here is a screenshot:

Screentshot

Lets get started with the code

Setting up the app

We build the app using the Express framework, and socket.io. To create a new express app use the following command in the shell:

express new <app_name>

Here are a list of dependencies required for the app:
package.json

{
"dependencies": {
"express": "2.5.1",
"jade": ">= 0.0.1",
"socket.io": "0.9.16"
}

Install all the dependencies using

npm install

 Setting up the game module

Now that you have Express set up, its time to develop the game module.

This function is used as a callback after reading the dictionary. We have a word_list.txt file which is a list of english words from the dictionary. We call it using

fs.readFile('word_list.txt', 'utf-8', init);

Handling socket events

In the setEventHandlers function we listen for new connections to Socket.IO and pass them to onSocketConnection function

Check out the code for onNewPlayer, onAddWord and onDisconnect from the repository.

In the next section we'll explore the game server fully and eventually start with the client side.
Cheers!

One thought on “Building a Real-time Two-player "Words in a Word" game with Node.js and Socket.io

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>