Down the JavaScript rabbit hole

I’ve always been really smart about computers, but programming is one thing that always eluded me. Every time I sat down to start, and try to learn, it was just too much, and I spent hours reading without really learning anything. Well, no more excuses, I’ve decided that 2012 is the year I am going to learn how to program.

If you know me, you know I love games, and games are what I want to make as a programmer. There’s lots of languages for writing games, so what’s the best one to pick? I used to think I knew the answer to that question, C++, but now I’ve changed my mind.

Since I want to make a startup around games I was looking for a way to write games that would make me money. This means a low barrier of entry, both for myself learning how to program, and also a low barrier of entry for publishing and selling games.

To make the most money the first thing I looked at was the iOS platform. I own an Android phone, but I know there’s more money to be made on iOS. I quickly learned the main language of iOS isn’t C++, it’s a completely different language called Objective C which only works on Apple devices (leave it to Apple to invent their own programming language). The more I read, the more I learned that I would probably have to learn both C++ and Objective C to write games for iOS, since you need to know Objective C for Apple, but most gaming libraries are in C++. Screw that, I’m going to have a hard enough time learning one language, let alone two!

Then came the real kicker: if you want to develop iOS apps, you must own a Mac, because the software to make iOS apps is only available for Mac! I’m a Windows guy and I have no plans on switching to Mac any time soon (I don’t like wasting money on inferior computers), so that was it. Plus the more I read I saw both Objective C and C++ have manual memory management instead of a garbage collector, and that sounds like a big pain in my ass! I want to be making awesome games, not manually managing memory!

So iOS was out. I got to talking with some of my friends who are programmers and they told me that you can develop games directly for the web with HTML5 and JavaScript. I was generally familiar with JavaScript, but I hadn’t heard anyone was writing games with it. Most of the games I had seen on the web had been written in Flash, and I hate Flash.

JavaScript as a language for games sounded interesting. I didn’t know much about JavaScript besides it runs in web browsers, and had not seen it had advanced new features for game authors, but I began checking it out and found something that completely blew my mind:

BrowserQuest is an MMORPG developed by Mozilla which you don’t have to download or install because it runs right from your web browser. It is written completely in HTML5 and JavaScript, and best of all it is multiplayer! People from all around the world can play at the same time, and it all shows up in realtime just like any installable game written in C++.

Then I realized something I hadn’t thought about: if I want to write a game like this, I will need to write a server too! Argh! What language am I going to use for that? I was already back to having to learn C++ and Objective C again. Can it run on Windows or do I need to figure out how to deploy on Linux? How do I make it talk to my game client running in the web browser?

As I dug into BrowserQuest more I made an amazing discovery: JavaScript can run on the server too. You can use one language to write the client and the server, and that language is JavaScript. The server-side JavaScript environment is called Node.js, and it runs perfectly on Windows so I don’t need to learn about Linux servers just to run my game server.

BrowserQuest is written in Node.js so I knew it would scale, and also reading about Node I found out it has a lot of advanced features like V8 JavaScript engine from Chrome, and event-driven non-blocking I/O which scales better than threads. Looking at the BrowserQuest status page it looks like it takes 4 Node.js servers to support about 200 people, which seemed like kind of a lot but doesn’t seem completely unreasonable. I didn’t plan on having that many people in my game right away, but I knew that one server would support at least 50 people.

I still wasn’t sure how to make the game talk to the server. I knew a little bit about AJAX, but that was so old that I assumed there’s something better out now, and there is. I discovered Socket.io, which makes it really easy for Node.js programs running on the server to talk to the client. In fact this is all you need to make it work:

SERVER

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

CLIENT

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

With my newfound knowledge that I could learn one programming language to write both the client and server of my game, I have decided that JavaScript is the programming language I am going to learn.

I am still looking for good resources on how to learn JavaScript, so if you know any good books you can recommend, please leave them in the comments as I’m a total n00b 😉

Leave a comment