Skip to content


Subversion checkout URL

You can clone with
Download ZIP

Getting Started

curraheerecursed edited this page · 27 revisions
Clone this wiki locally

Getting Started with NowJS


NowJS is a node.js module, which means you'll need node installed before proceeding. If you don't, see the node.js website for an introduction and instructions on how to get it.

After installing node, you'll need to grab npm (a node.js package manager). Once npm is installed, you can type "npm install now" at the command line and get access to NowJS.

Basic Idea

NowJS provides a 'now' object that both the client and the server share access and control. A 'now' object is created for each client when it connects to the server. 'now' acts as a regular Javascript object, what's special is that both the client and the server can modify it, ie. store data and define functions, while the other side can use it.

There is also an '' object that is only seen by the server. It gives access to all 'now' objects of connected clients simultaneously.

Build Your First NowJS Server

Here is a fast demonstration on how to write a simple chat server using NowJS. In this example you will see how 'now' is used for information sharing. You can find the code here.

Setting Up

The "helloworld_server.js" file starts with the following lines:

var fs = require('fs');
var server = require('http').createServer(function(req, response){
  fs.readFile(__dirname+'/helloworld.html', function(err, data){
    response.writeHead(200, {'Content-Type':'text/html'});

These lines are required to set up the server and prepare the "helloworld.html" file for the client.

Creating 'everyone'

var nowjs = require("../../");
var everyone = nowjs.initialize(server);

This is where the the server creates the 'everyone' group. Notice that no 'now' object exists right now; instead, they will be automatically created when clients connect.

Keeping Track Of Clients

everyone.on("connect", function(){
  console.log("Joined: " +;

everyone.on("disconnect", function(){
  console.log("Left: " +;

Whenever a client connects or disconnects, a log will be recorded in the console. Notice the use of '' here. '' refers to the 'now' object that belongs to this specific client, while 'name' is a field created by the client (as we'll see in a minute), that can be accessed here on the server. This step is optional; it doesn't affect the behavior of neither the server or the client. But it's nice to have it written down.

Sending Out The Message = function(message){, message);

Here is the tricky part. We see two functions, namely 'distributeMessage', which is defined here on the server, and 'receiveMessage', whose definition is not given. For now all we need to remember is that 'distributeMessage' calls 'receiveMessage'

Something went wrong with that request. Please try again.