Chapter 2. Setting Up the Environment

The goal of the last chapter was to introduce you to multiplayer game programming in JavaScript using current HTML5 technologies. Although we went over the implementation of a real multiplayer game, there was no mention made of how you might manage a more complex project.

Along with new technologies such as WebSockets, we can also attribute the great advance that has taken place within the web platform to the supporting tools that have been created to support project management and workflow of HTML5 and JavaScript development.

In this chapter, we will discuss the following principles and concepts:

  • Developing JavaScript applications in Node.js
  • Writing modular JavaScript applications
  • Managing Node.js packages with npm
  • Managing client-side packages with Bower
  • Automating JavaScript development

JavaScript outside the browser with Node.js

It wasn't too many years ago when a so-called web developer used JavaScript on the rare occasion when a web form needed client-side validation. Since CSS wasn't as advanced as it is today, or at least it wasn't widely supported, JavaScript was also used in order to create image rollover effects. Not many years ago, the words JavaScript and programmer would not have gone well together.

However, times change and technologies evolve. Today, qualified JavaScript programmers are sought after and compensated very competitively relative to programmers of other programming languages. This is a reflection of how popular and powerful the JavaScript language has become.

As a result, JavaScript is steadily going from being The World's Most Misunderstood Programming Language (Crockford, Douglas (2001). http://javascript.crockford.com/javascript.html) to becoming an enterprise-level language, which is used in applications both inside the browser as well as in standalone programs, including server applications. As explained and illustrated in the last chapter, JavaScript is used in different ways when it is employed for the client-side build of your game as well as the game server.

You may remember that a game server doesn't have to be written in JavaScript. In fact, the game client has absolutely no idea what language the server is written in since all of its communication with the server takes place through the WebSocket protocol. However, since we want to maximize the amount of code that we can share between client and server, while reducing the overall amount of code that we write at the same time, we will write our games in a way where this sharing of code is possible. That is where Node.js comes into play.

Node.js

Without doubt, you've heard of Node.js by now. For those who are not exactly sure what Node actually is, it is simply a runtime environment built on Google Chrome's JavaScript engine (also known as V8). In other words, Node is neither a special version of JavaScript nor is it a standalone JavaScript engine, but rather, it is an entire ecosystem that happens to leverage Google's open source JavaScript engine, which happens to be, arguably, one of the seven wonders of the world today.

Node.js

Two characteristics of Node.js that are worth mentioning are that Node.js is not tied to the browser, and every I/O operation is asynchronous.

As for it not being a browser environment, you will not find a window object like you do in a browser. In addition, since none of the restrictions that are imposed by a browser exist in the Node.js environment, you can take full advantage of the underlying operating system. First, think of whatever server-side language you have been using so far, or whatever programming language you were considering using to write your game servers that we discussed in Chapter 1, Getting Started with Multiplayer Game Programming. Then, replace that language in your mind with JavaScript. This is the big offer that Node.js makes.

Some of the benefits that you will find in using JavaScript on both ends of the stack (server side and client side) include the following:

  • You can share a lot of the code that you write for the server and client
  • You only need to master one language
  • JavaScript is a powerful language that solves many of the problems that exist in other languages
  • Since JavaScript is single threaded, you will never have deadlocks or many of the issues associated with multi-threaded programming

By now, I hope that you are able to see how fundamental Node.js can be in HTML5 multiplayer game development, or at least how crucial it will be in this book. Before we dive too deeply into some of the fundamental concepts, let us ensure that you can install and run it on your system.

Installing Node.js

The two recommended ways to install Node.js on your system are to download an executable file from the official website at http://www.nodejs.org or to install it manually by compiling the source code. Depending on your operating system of choice, you may also be able to install it via some package management system or a similar tool. Whatever method you decide to pursue, be sure to install the latest stable version, which, as of this writing, is Version 0.12.0.

Once you have installed Node.js on your system, you can take it for a test run by opening a terminal window and typing in the following commands:

node
console.log('Hello, World!'),

If all goes well during the installation process, you should see an output similar to the one displayed in the following screenshot:

Installing Node.js

You can check the version of Node.js that you have installed by running the following command on your terminal:

node --version

Even though the latest version available today (as of this writing, in early 2015) is 0.12.0, all scripts described in this book are written in and tested against Version 0.10.25. For backward-and forward-compatibility issues and questions, be sure to reference Node.js's official backlogs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.220.16