Description

Note This tutorial has been updated 2014-08-28 to support Meteor 0.9.

We will build a small chat application based on Meteor. It will support anonymous users and users signed in using their GitHub account.

The full example code can be found at https://github.com/sebdah/meteor-chat-tutorial.

The tutorial will cover the basics of:

  • Meteor
  • Meteor Accounts (user management system)
  • Handlebars usage, as that is the default templating system

Before we begin, please open a tab with the Metor Docs. It’s a fairly good companion throughout the tutorial.

Install basic requirements

Install Meteor

First off, install Meteor itself by entering the following in your terminal.

curl https://install.meteor.com | /bin/sh

It will download and install Meteor for you.

Building the application

Alright, now that you have the basic components installed, let’s get started with the actual development.

Project structure

Let’s create the project

meteor create chatapp

This command will create a basic Meteor project structure that may suit less complex web apps, but it tend get messy as the projects are growing. The default structure (which you have in your chatapp folder) looks like this:

chatapp.css
chatapp.html
chatapp.js
smart.json

The problem is that code for both the client and server lives is within one .js file with an if clause to separate code from the client and server:

// Shared code goes here

if (Meteor.isClient) {
  // Client code
}

if (Meteor.isServer) {
  // Server code
}

But Meteor offers an alternative project structure that we will use in this example. So, before we begin let’s remove some of the example code generated by the create command.

cd chatapp
rm chatapp.*

Now create two folders instead:

mkdir client server

Your chatapp folder should now have the following folders and files:

client/
server/
smart.json

As you might have guessed, all code that lives within the client directory will be available for the client (i.e. the browser), while all code under server will remain on the server side. It is easier (in my opinion) to structure the code like this when the code base grows.

Writing the HTML template

Here’s the basic HTML template that we will be building on top of. There are a few things to note here. First of all, there’s no doctype or html tags because Meteor will add that for us. Secondly there are some wierd tags like {{> welcome }}. This is all part of the Handlebars templating system. What is says is that

The template with name="welcome" should be inserted at {{> welcome}}

Store the HTML template in a file called client.html in your client folder.

You can now launch your Meteor app with

cd chatapp
meteor

The point your browser at . You should see something like this:

You can leave Meteor running, it will automatically update whenever you change a file in the application.

Implementing simple chat functions

We’ll start writing the actual code for sending messages and we will use the built-in MongoDB database to store all messages.

Create a file called models.js in the chatapp.

This will define a new MongoDB collection called messages. Now let’s extend our client.html to support messages. Add a new {{> messages}} tag right under {{> welcome}} and add the following