Sunday, August 5, 2012

Making a game data server with Node JS

Hey guys,

So this is going to be a new series of tutorials that I'm going to run that are based in the world of Node, mobile gaming and network data delivery and database management. I realise that this is quite far removed from the tutorials that I was running before, but as many of you may be aware, my current work has taken me further away from Flash and Game programming and into a more supportive role with back-end programming and database services taking up the greater percent of my brain power. So subsequently, I'm going to be writing tutorials based around this area.

I briefly mentioned in my last post that I was going to try and use Tuts+ to publish my Tutorials, however, I have had some issues with trying to submit some things to them and so am now going to be running them both here in text form and hopefully screen casts of them on my youtube account also.

This tutorial is going to go over the basics of the software that we'll be using, how to get things moving with Node, and what exactly we're going to be doing with Node.

Node is a Java based server technology, which is very both in terms of running as well as development iteration. The purpose of Node in these tutorials is to provide an end-point for an arbitrary application to connect to to ask for some data. The best way to perhaps think about Node is that it is a gateway that sits somewhere on the internet (or in the case of this tutorial, a port on your computer) and awaits incoming requests and then does something when it gets a request.

Node can be downloaded from here - depending on the operating system you're on, you'll want to get the right version for you. From the experience that I've had, Windows is a bad development environment for this sort of thing, but still works, I work from a Mac and Linux platform, however, the code and skills that you'll acquire from these tutorials are transferable as they're all based in the same language - Java.

Node comes in a installation package, so once you've downloaded it, install it onto your computer, and that's pretty much all you need to worry about for installing. Once you've done that - you'll want to open up Terminal - we're going to go Matrix up in here. Terminal is where you'll be running your Node server from and frankly, Node is a very good way to get to know the way terminal works and the basics behind a command line interface. As I'm working in a Mac environment, the commands that I'll be issuing will be for Mac CLI (Command Line Interface) again, the Windows and Linux commands are fairly similar, e.g. cd in Mac = dir in Windows.

The first thing you'll want to do is create a folder somewhere that you'll want to put your Node server.

$ mkdir SPGames_Tutorial1 
$ cd SPGames_Tutorial1 

This will create a folder on your computer, and then change directories into it. Once you're inside this new directory, you'll want to do one of several things, depending on whether you want to use a simple text editor like VIM or a more complex GUI like EMacs or Notepad++, personally, I use VIM, so the command that I issue is:

$ vim app.js

This will start a new file in VIM, and open up the VIM editor in your Terminal window. If you haven't used VIM before, the controls can be kind of hard to understand and seem quite unintuitive, but after a while, you'll get used to them and be able to navigate through huge files very quickly. The few commands and key strokes that you should know for this tutorial are:

"i" - enter insert mode (Basically, you need to hit this to type anything onto the screen)
"Shift + ZZ" - Save and close a file. Alternatively, if you want to save, but don't want to close the file, you should
":w" - This command will only work if you are outside of insert mode, to exit insert mode, you simply tap escape.

There are literally hundreds of keystrokes and keystroke combinations that VIM allows that allow you to look for things, go to the end of a word, end of a line, start of a line, find a matching parenthesis, the list goes on. But these ones will get you start.

So, once you're inside VIM, you'll want to type out the following. Now, this is where one of my cardinal rules of tutorials comes in, if you're following this tutorial, I seriously urge you to put your Terminal window on top and type out the code in your own screen as opposed to copy & paste it. It is of course up to you,  but I tend to find that I really don't hold onto much information  if I just haphazardly copy and paste things in, and when things go wrong and it doesn't work - then I don't know what on earth is happening because I didn't write the code out myself.

Stepping through this code:

Line 1: Creates the "http" variable, the require('http') grabs the http library from node for use.
Line 2: Create a server with our new http variable, the createServer method takes a function argument, with two parameters, req & res. Req is the request, and of course res is the response.
Line 3: Writes some header information to the response object.
Line 4: Ends the response object with a little bit of writing.
Line 5: The .listen method of the http object sets the server up in terms of what ip address and port you want the server to listen on. Here it's listening on local host and 1337 for port.
Line 6: console.log is a great way to test the server when you need to debug and know what's going on in terms of contact with the server.

Now that we've written on the code of the Node server. Save the file in VIM by hitting Shift+ZZ. Then to start the server up:

$ node app.js 

Once the server has started, you'll want to keep that Terminal window open or the server will shut down. Using your favorite browser, navigate to the address that you specified in the Node app, in my case http://127.0.0.1:1337. Here you'll see the message that you put into your node app when you wrote it out.

That's all that I wanted to put into this tutorial, it's a slash dash start-up and introduction to Node, and from here we're going to cover a few more complex subject. Most importantly, how Node is going to server as a game server for holding information and some more complicated