How to Get a Node Joyent SmartMachine running with MongoLab

April 09, 2013 - by Ben Wen

This is a basic how-to video and article on getting a Node.js Joyent SmartMachine running with MongoLab, a fully-managed cloud database service featuring highly-available MongoDB databases, automated backups, web-based tools, 24/7 monitoring, and expert support.

The combination of MongoLab with Joyent’s Infrastructure-as-as-Service offering allows development teams to focus on their core product offerings instead of database administration and operations. Learn more by reading on, or simply watching the accompanying video tutorial here.

Demo Overview

The demo application is called TractorPush. It uses a tailable cursor and WebSocket , using Socket.IO, to push data from the server to the browser.

The Reader process has a rudimentary web server front-end that includes a Javascript app which opens a Socket.IO connection back to the server. Because we’re here at the Joyent Cloud, high-performance Web Sockets are available. Socket.IO is clever enough to negotiate other mechanisms for the push, often an XHR-long poll in other Infrastructure-as-a-Service providers.

node-mongolab

In our Tractor Push demo system, we’ll stand up a Writer process that creates the capped collection and writes a new document every second. The document or message types are chosen randomly from one of three types: ‘simple’, ‘array’, and ‘complex’. We’ll use a tool from Nodejitsu called ‘forever’ to keep that process running in the background.

The Reader process opens two tailable cursor queries, one that filters for ‘complex’ messages, and one that accepts all message types. It sends back these messages to the browser over WebSockets at a rate of about 3 per second.

Tailable cursors in MongoDB are available on capped collections. They have the unique property of blocking when a query’s cursor is exhausted and unblocking if new data is inserted that matches the query. This, combined with WebSockets, makes for a nice dynamic push-style queuing system. Arbitrary document schema can be inserted, filtered, and forwarded.

All code for our demo system is open sourced and available at GitHub. Read on to find out exactly how to make this demo work.

Demo How-to Guide

Prerequisites:

  1. Open a MongoLab and Joyent Cloud account
  2. Setup an SSH key
  3. Install Node.js (tested with 0.8.11) http://nodejs.org/
  4. Install Joyent's Smart Datacenter tools

Steps:

  1. Create a MongoLab free database at Joyent’s us-east-1 datacenter by visiting MongoLab
  2. Note the MongoDB URL and MongoDB database credentials (distinct from the MongoLab login)
  3. In your comannd line shell, bind to the us-east-1 API with sdc-setup https://us-east-1.api.joyentcloud.com (optional, set environment variables SDC_CLI_URL, SDC_CLI_ACCOUNT per on-screen instructions)
  4. sdc-createmachine -n <make a unique name> --dataset "sdc:sdc:nodejs:1.4.0"
  5. Note the IP address while waiting for the VM to be provisioned.
  6. ssh into that ip address with ssh -l root <ip address>
  7. git clone http://github.com/mongolab/tractorpush-writer-node
  8. git clone http://github.com/mongolab/tractorpush-server
  9. cd tractorpush-server
  10. npm install
  11. cd ..
  12. cd tractorpush-writer-node/
  13. npm install
  14. npm install -g forever
  15. export MONGOLAB_URI=mongodb://<USERNAME>:<PASSWORD>@<URL FROM STEP #2>
  16. forever start app.js
  17. cd ../tractorpush-server/
  18. export PLATFORM=joyent; export PORT=80
  19. node app.js
  20. Point your browser to <ip address>

Thanks for reading! You can reach me, Ben Wen, on Twitter @benwen.

:

Sign up Now for Instant Cloud Access

Get Started

View PricingSee Benchmarks