Wednesday, February 29, 2012

Juggernaut in Windows

I've fallen in love with Juggernaut.
Juggernaut gives you a realtime connection between your servers and client browsers. You can literally push data to clients using your web application, which lets you do awesome things like multiplayer gaming, chat, group collaboration and more.
It's built on node.js, socket.io, and Redis and makes implementing PubSub in your web apps super easy. (How easy is it?) This easy:

Client side (subscriber)
var jug = new Juggernaut;
jug.subscribe("channel1", function(data){
alert("New message: " + data);
});
Server side (publisher - I'll use Python, but Ruby is similar)
jug = Juggernaut()
jug.publish('channel', {'message': 'Hello World!'})
I use Juggernaut to implement real time features in my Flask application. Flask is a traditional web framework and not very well suited to real-time. Juggernaut is a great complement. My development environment is Windows, so I needed to get Juggernaut running there. It wasn't terribly difficult but there were enough gotchas that I thought I'd share how it's done (and that it can be done).

This assumes you have Python 2.7 installed. I would not recommend deploying Juggernaut in production on Windows. Here we go:
  • Get Redis
    • Redis isn't supported on Windows but there is a great native port available that seems to work well enough. I wouldn't run it in production, but it's been stable for me in development. Download it here and extract to a folder anywhere you please.
  • Get Node.js
    • Node now supports Windows! Download and install it here.
  • Get Juggernaut
    • You'll install Juggernaut via the Node Package Manager (npm). Npm is installed by default with Node. Open a command window and navigate to the install directory for Node (this is C:\Program Files (x86)\nodejs on my machine). Run npm install -g juggernaut
  • Get the Python library
  • Run it!
    • Fire up redis-server.exe from the directory that you extracted Redis to.
    • Run juggernaut from a Windows command line (just type juggernaut)
    • Juggernaut helpfully comes with a nice little example implementation (index.html). Npm installs it here by default: C:\Users\\AppData\Roaming\npm\node_modules\juggernaut\public
    • Not-so-helpfully, the test implementation connects to Juggernaut over port 80, which is silly because Juggernaut serves messages over port 8080. So crack open that index.html file and change line 37 from "80" to "8080"
    • Next, copy index.html and application.js (which is the client side Juggernaut library) to a directory in IIS so you can hit it locally. You do need to serve the file from a web server - you can't just open the static file in a browser because when the sample tries to connect to Juggernaut the browser will view it as a cross-domain request and reject it.
    • Now, browse to http://127.0.0.1/index.html:
    • Almost done! Finally, fire up IDLE (or a python interpreter) and try the following. You'll see the message immediately in the client app. Cool!

      Hope this helped some folks. Enjoy your shiny new real-time web component!

    No comments:

    Post a Comment