Meteor & Compose: a Beginner’s Guide to Using Them Together

For International Meteor Day, marking the JavaScript framework/platform reaching version 1.0, we are going to show you how to use Meteor and Compose together.

When you want to use Compose with the Meteor framework, there are only a few things you need to do to switch from using Meteor's local single server MongoDB to our auto-scaling, replicated, backed-up and managed, production-ready MongoDB. Meteor has a great tutorial at https://www.meteor.com/install and what we'll show you here is what you need to change at each step to use Compose with that tutorial. So...

Step 0: Installing Meteor – There are no changes in how you install Meteor. But this is the time to make sure you are signed up for a Compose Elastic Deployment MongoDB. We'll create a new database called meteordb for this example. If you are already signed up with Compose, just click Add Deployment in the dashboard and fill in the form...

Screenshot 2014-11-05 11.45.46

Then click "Add Deployment" in the bottom right of the form and the Compose system will go and deploy a multi-node, autoscaling MongoDB database for you. Once that's created, you should see the database's Collections view.

Screenshot 2014-11-05 11.53.28

Click on the Admin tab to see the various administration tools. The first thing you'll see is the overview which includes information about how to connect to your database.

Screenshot 2014-11-05 12.32.52

In our example that is:

Replica Set URI  
mongodb://<user>:<password>@candidate.43.mongolayer.com:10279,candidate.42.mongolayer.com:10195/meteordb

Mongo Console  
mongo candidate.42.mongolayer.com:10195/meteordb -u <user> -p<password>  

Both of these settings need a user and password configured for them to work. Click on the Users and then the Add user button. We'll add a user called 'meteoruser' with a password 'meteorpass' - do try and pick a less obvious password when you do this. Next, this user will need access to the Oplog because Meteor's MongoDB driver uses that to rapidly work out changes. Click on 'oplogAccess' and your display should look like this:

Screenshot 2014-11-05 12.36.46

Click the lower 'Add user' button and Compose will add that user for you. We now have everything we need to move onto the next step in the tutorial.

Step 1: Creating an app - The first part of this, running meteor create simple-todos you can do. Meteor will create your application. But, before you go and run the meteor application itself, you need to set some environment variables to tell it how to connect to MongoDB, MONGO_URL and MONGO_OPLOG_URL. The first of these is simple enough. Recall the Replica Set URI we go from admin page. All we need to do with it is substitute in the user and password from the user we created and set that as MONGO_URL:

export MONGO_URL=mongodb://meteoruser:meteorpass@candidate.43.mongolayer.com:10279,candidate.42.mongolayer.com:10195/meteordb  

The second environment variable needs to be mostly the same as that, with one change. At the end where it gives the database name – meteordb – we need to insert local?authSource= followed by the database name. Some shells may try and expand the ? in there so best wrap the value in double quotes. This gives us the following:

export MONGO_OPLOG_URL="mongodb://meteoruser:meteorpass@candidate.43.mongolayer.com:10279,candidate.42.mongolayer.com:10195/local?authSource=meteordb"  

You'll probably want to add those two items to a shell script or copy them into your profile if you are only working on one project. Once you've done that, you can continue through the rest of step 1, and step 2 and onwards...

Step 3: Storing tasks in a collection – You will be able to complete the first part of this step with no problem, but for the second part, "Inserting tasks from the console", you'll have to deviate from the tutorial slightly. This is because the tutorial suggests you use the meteor mongo command to connect to your MongoDB. The problem is that that command only works with local or Meteor-deployed instances of MongoDB. It's a bit of an oversight, and it means you'll have to install the Mongo shell on your system to connect up and to do that, you'll need to usually need the rest of MongoDB with it.

The quickest ways are covered in the Install MongoDB manual pages. If you are on Mac OS X and have Homebrew installed, it's simply a matter of running brew update; brew install mongodb while on most Linux distributions you just need to select the distro's MongoDB package. Whichever route you choose, by the end you should have the mongo command available, and thats the shell. Back at step 0, you may recall that there was also the "Mongo Console" entry in the connection setup. That's the command we need now, with the meteor username and password we are using substituted in. For our example, it looks like this:

mongo candidate.42.mongolayer.com:10195/meteordb -u meteoruser -pmeteorpass  

You'll get a Mongo shell prompt and you can now continue the tutorial and insert some tasks using the command line (and be impressed by how quickly the web browser updates when you insert a records into the database).

You can now work through step 4 (Adding tasks with a form) and step 5 (Checking off and deleting tasks).

Step 6: Deploying your app - The good news is that what is in this step works. Your application will be deployed up to a meteor.com sub-domain for testing. The bad news is that it won't connect to your Compose database because the testing system doesn't support external databases. You'll be able to deploy and enjoy, but you'll have to select some other deployment route, be it your own server or a hosted cloud instance, to run your app on the Internet. The instructions for step 7, running your app on Android or iOS, will also work apart from where it suggests you point the mobile app to the server you haven't deployed.

The final four steps, 8, 9, 10 and 11, need no intervention and you can work through them without changes. That'll bring you to the closing step 12 which directs you onto some of the other examples available and onwards to creating your own applications using Meteor. The additional steps that we have introduced here won't make any meaningful difference to how well the examples run until you start running a second Meteor server and get that talking to the MongoDB database too. By applying those changes from the start though you'll be ready to scale up from day one, just like Compose's Elastic Deployments of MongoDB already do.