Juggernaut Tutorial

2010-09-13 00:00:00 +0100 by Alex R. Young
This article is by [Alex MacCaw](http://www.eribium.org/). He's a JavaScript and Ruby developer and his blog is [Lead Thinking](http://leadthinking.com/). Follow him on Twitter here: [@maccman](https://twitter.com/maccman).

Juggernaut gives you a realtime connection between your servers and client browsers. This lets you do
awesome things like multiplayer gaming, chat, group collaboration and
more. What's more, Juggernaut is built on top of
node.js so you can take advantage of its incredible speed and scalability.

Example code is worth a thousand words, so here's a simple group chat
implementation using Juggernaut:

   var jug = new Juggernaut;
   jug.subscribe("/chats", function(data){
     $("#chats").append($("<li />").text(data));

That's it, the browser will set up a streaming connection to
Juggernaut's node server. Any messages pushed to the channel "/chats"
will be sent to the client. For example, we can push data from a Ruby

require "juggernaut"
Juggernaut.publish("/chats", "Hello World!");

That's all the code you need - all clients connected will see the

Juggernaut's features include:


So you want to try it out? We need to install some dependencies first:

# Install node.js

Right, now we need to start "Redis":http://code.google.com/p/redis/ ("install":http://code.google.com/p/redis/wiki/QuickStart it if you don't have it already). Juggernaut uses Redis for PUBSUB:

./redis-server redis.conf

Download Juggernaut, and start the Juggernaut server:

git clone git://github.com/maccman/juggernaut.git
cd juggernaut
node server.js

That's it! Open http://localhost:8080 and you'll see Juggernaut in


Now we've got our client running, let's publish to it. We need to
install the Ruby gem to do so (don't forget the --pre option!):

sudo gem install juggernaut --pre

And now in irb:

require "juggernaut"
Juggernaut.publish("channel1", "Yo yo yo")

You can also publish to multiple channels:

Juggernaut.publish(["channel1", "chan2"], "yo yo, my good fellow")

And send objects:

Juggernaut.publish("channel1", {:where => "is my yo yo?"})


So, that's a brief introduction to Juggernaut. You can check out more
use cases in the README. Now go
and build something awesome!