Listening to a server heartbeat using Node.js, iOS, and Socket.IO (Part 2)


At this point, after following the first part of the tutorial, your iOS app should already be set up with the server located at

In this part, I'll cover how to use Node.js and Socket.IO to recreate this functionality on your own server!


This tutorial will cover the installation of Node.js on a Mac (specifically OS X Mavericks 10.9.2).

However, once Node.js is installed, the remainder of the tutorial will be the same if you're running on Linux or (dare I say?) Windows.

This part of the tutorial will use the following tools:

Homebrew / Node.js Installation

To install homebrew, simply open the Terminal app, which you can find using Spotlight and paste the following command:

ruby -e "$(curl -fsSL"

The process is fairly straight forward and guided by the installer.

Once the installation is complete, be sure to run this command:

brew doctor

Finally, run brew update && brew install node.

To verify everything worked, type node -v to get the Node.js version number.

Getting Started with Sublime Text

Once homebrew and Node.js are installed, you can run the following command in Terminal (assuming you've copied Sublime Text to your /Applications folder).

ln -s /Applications/Sublime\ Text\ /usr/local/bin/

To verify this worked, type subl into the Terminal app and hit enter. If everything worked properly, Sublime Text should open.

Return to Terminal and run the following commands:

cd ~/Desktop
mkdir heartbeat
subl heartbeat

This will create a new folder called "heartbeat' on the Desktop and open sublime text, using the new folder as the root directory.

Right click on "heartbeat" in the FOLDERS menu and create a new file with the following contents:

    "name" : "HeartbeatServer",
    "description" : "A simple server that sends a heartbeat every second to all connected devices.",
    "private" : true,
    "version" : "1.0.0",
    "dependencies" :
        "" : "*"

Save the file with the filename package.json (no caps).

The format of the file is fairly straight forward. It declares the project's name, description, version number, and dependencies. In addition, by setting the private key to true, we'll make sure that this code doesn't get pushed to the public repository of Node.js packages.

Once this is done, return to Terminal and type npm install. This will use the Node Package Manager (npm) to install all of the dependencies (or modules) for our project.

Implementing the Server

Next, in Sublime Text, you'll want to create a new file called server.js in the heartbeat folder.

The first step is to create two variables for the port we want to run the server on and the color counter. The iOS app is expecting values 0-3, so we need to keep track of the counter globally if we want to keep all connected devices in sync.

var port = 3333;
var counter = 0;

Next, we'll create a Socket.IO server:

// Create the Socket.IO server and tell it to listen on the port declared above.
var socketServer = require('').listen(port);

Next, create a namespace, or channel, for iOS connections:

// Create a new namespace on the socketServer for iOS connections.
var iOS = socketServer.of('/ios');

Next, we need to create a function to update the devices by sending out a heartbeat to all of the devices on the /ios namespace:

function updateDevices() 
   // Check to see if the count is greater than 3 - if so, reset it to 0.
   if(counter > 3)
        counter = 0;

   // Send an "updateColor" message with the current counter
   // to all connections on the iOS namespace.
   iOS.emit('updateColor', counter);

   // Increase the counter by 1

And lastly, we need to make sure that this function is called on a regular interval:

// Call the updateDevices function every 1000ms
setInterval(updateDevices, 1000);

When all is said and done, your server.js file should look like this:

Heartbeat 9

Running the Server

The final step is to actually run this server on your own hardware!

Back in Terminal, inside the heartbeat folder, run the following command:

node server.js

This will start the heartbeat server. You should see some debugging code like this:

Heartbeat 10

The final step is to go into the iOS app project in Xcode and change the -viewDidLoad: method to look like this:

- (void)viewDidLoad
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _socketConnection = [[SocketIO alloc] initWithDelegate:self];
    [_socketConnection connectToHost:@"localhost" onPort:3333 withParams:nil withNamespace:@"/ios"];

If you're not familiar, localhost is used to tell the machine the server is running on the local machine (or host), and the port is 3333, which corresponds to the variable we created in server.js.


With any luck, that will have worked! The heartbeat server should be running on your computer and the iOS simulator should be able to connect! If you're running on a physical device, you'll need to replace localhost with your computer's IP address.

I've uploaded my fully working server (along with the updated iOS project) here in case you run into any trouble.