Sharing a Javascript base class between Node.js Server / Client-side Browser

I’ve been using Node.JS a lot lately, and Im really enjoying it.

What is it / How does it work?

I was very hazy about that a few weeks ago other than, “It’s some kind of server and also a buzz word”
Node.js is a very very fast event-based server. Also equally important, the server-side code you write is itself written in javascript.

One of the good things about Node.js is that It has a strong following already. People have created many modules for it already. One such module is a Websocket implementation.

What might I use it for?

We are using it here at work to create an HTML5+Websockets real-time multiplayer game.

In order to make this game work, I have the server running a version of the game – while each individual client is running in their browser a version of the game as well. The server is ultimately the one in charge of killing players / tracking projectiles.

The users merely see a facade of this game they play. Their own character sends information to the server, which they implement in themselves instantly. For example when you fire a projectile, you send that information to the server (what / how fast / what angle). Once the server receives that message, and adds that projectile to it’s running of the game. You see the projectile move, but really the server is now in charge of it and it will let you know when it has hit another player. Other users characters movements are predicted based on the last received information, but continuously checked as new information is received.

For this methodology to work, the server needs to have a version of the game running. This is only viable if the server and client side can share base classes between one another. For example the character/projectile classes are no different on the client or server. We share both between them.

However ClientGameController and ServerGameController differ from one another, yet both inherit from AbstractGameController.

This is the beauty of node, it is very easy to do this and keep the code looking nearly exactly the same on both sides if you use Require.js.

Let’s see it in action

This is a shared Character class implementation.

var init = function()
	// some defaults we use
	this.velocity = { x: 0, y: 0 }; // how fast i am going
	var CharacterController =  function(aClientID) {
		this.clientID = aClientID;
		console.log('Character ID:', this.clientID); // Node has a console.log so this works perfectly on both.
	CharacterController.prototype.doSomething = function() {
		console.log('(CharacterController) doSomething()!');
		return 'hi';
	return CharacterController;

if (typeof window === 'undefined') {
// We're in node!
	exports.CharacterController = init();
} else {
// We're on the browser. 
// Require.js will use this file's name (CharacterController.js)
// to export an object called - CharacterController

      // You could use this to require modules before your class is created
      define(['ClassCharacterUses', 'WeaponClass'], 'CharacterController', init); 

We are taking advantage of the fact that the browser has a global ‘window’ property. Another thing to note, is how similar Require.js is to Node’s own require syntax.

2 thoughts on “Sharing a Javascript base class between Node.js Server / Client-side Browser”

  1. Pingback: Südtirol: Tipps
    1. Unless you invented functions and variables, i doubt it – this except if anything is from BonsaiDen’s code in his BiSON encoder.

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.