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.
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.
sdc-setup https://us-east-1.api.joyentcloud.com(optional, set environment variables SDC_CLI_URL, SDC_CLI_ACCOUNT per on-screen instructions)
sdc-createmachine -n <make a unique name> --dataset "sdc:sdc:nodejs:1.4.0"
ssh -l root <ip address>
git clone http://github.com/mongolab/tractorpush-writer-node
git clone http://github.com/mongolab/tractorpush-server
npm install -g forever
export MONGOLAB_URI=mongodb://<USERNAME>:<PASSWORD>@<URL FROM STEP #2>
forever start app.js
export PLATFORM=joyent; export PORT=80
Thanks for reading! You can reach me, Ben Wen, on Twitter @benwen.