An Introductory Meteor Tutorial, Improving On The Leaderboard Sample App


In this tutorial I’ll discuss my experience developing and launching(and yes you’ll see how easy it is to launch with Meteor) a realtime javascript application using the Meteor framework; with its built in MongoDB shell and strong client server architecture Meteor is a powerful framework that should become popular for developers wishing to build fast realtime web applications.

To begin this tutorial the first thing you’ll want to do is to install the Meteor framework for NodeJs.

Due to the nature of the framework under going constant updates, I won’t write down specifically how to install Meteor because I’ll then be forced to update this tutorial whenever the installation procedure changes.

However I don’t see anything wrong with providing a link to the official Meteor documentation where you’ll find the download and installation instructions readily prepared for you. Meteor Installation Guide Once you have Meteor installed on your system you’re ready to go. If you’re running a Linux variant or Mac OSX then then the installation guide should be sufficient however, Windows users might require extra steps.

Worry not though because if all else fails for getting Meteor running on Windows I suggest you just download and install a free copy of Ubuntu Linux and get it running first and then return to this tutorial.

So now that you have Meteor up and running we’re going to start off by downloading the sample application “Leaderboard”. To do so go to the Meteor official documentation for the leaderboard sample app and follow the instructions Meteor Leaderboard. Once you have read through the instructions go ahead and give the sample application a test drive by changing into the directory of the application and entering “meteor” into you terminal window.

Next the application should begin most likely on localhost:3000 which you can navigate to from your web browser of choice and view the application running live.

Ok so far we have installed the Meteor framework; downloaded the sample app, ran the sample app and verified that we can navigate to the application from our browser and see it in action. Now we’ll begin modifying the leaderboard so we can better understand the framework and take a gander into its design structure.

From your terminal window open the directory where you placed the sample app; you should see three files leaderboard.css, leaderboard.html and leaderboard.js.

Based on the file names you can easily tell that the first file we’re going to edit is the leaderboard.html since its the file which connects both the css and javascript. Go ahead and open up leaderboard.html with your editor of choice.

The re-branded sample application i.e fork of the leaderboard app we’re going to create is called Boozenet. Boozenet was the name of this application chosen the team I worked with during a previous Hackathon.

Boozenet was an application developed for the sole purpose of allowing friends who drink together on nights out to keep track of not only the beer they drank but the total amount of calories. This application was developed during a mobile Health themed hackathon so the caloric intake was supposed to be the main feature of the app but the fact that it was realtime and ran on mobile as well as web based clients was the most impressive feature after all. Enough background, within leaderboard.html find the and update its contents to appear as below.

<head>
  <title>BoozeNet</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>

In addition Meteor also has a custom jquery plugin, from the documentation to use the jquery plugin navigate to the directory of this application and run the following command from the terminal “meteor add jquery”.

So far we’ve updated the title of the application and included jquery… not much but its a good start. Moving onto the tag you’ll notice the unfamiliar (if you’ve never used an HTML templating framework before) set of double curly braces.

What these represent are places where we’ll substitute sections of HTML when the content is rendered on the browser once the templating javascript code scans over and updates the sections of the HTML marked with double curly braces when the application is ran. Despite the large number of HTML templating engines available (just Google for it and you’ll see) the one pre-baked into the Meteor framework is a modern templating engine called Handlebars

Click here for the Handlebarsjs Homepage.

If you want you can read all about how Handlebarsjs does its magic by browsing the its homepage but for now we’ll focus on applying it to extend the leaderboard sample application.

<body>
  <div id="outer">
  <table>
  <tr>
  <td><img src="beer_vector-200.png" /></td>
  <td>{{> game}}</td>
  </tr>
  </table>
  {{> gallery }}
  <div class="none" />
  {{> leaderboard}}
  </div>
</body>

First we’re going to explain how the gallery template works; once that is established hopefully the double curly brace syntax won’t appear so alien and you’ll start reading it as just another HTML element. What the gallery does for our application is showcase the snapshots (in this contrived example they will be hyperlinks to images on the web) of various beers that users of our app have consumed. All this really means is that the gallery template is a placeholder for images to be dynamically inserted at when the page is rendered from image links stored within out database.

<template name="gallery">
<div class="gallery">
{{#each photos}}
{{> photo}}
{{/each}}
</div>
</template>

As you can probably already guess the