Skip to content

Instantly share code, notes, and snippets.

@jadeapplegate
Last active March 3, 2016 09:14
Show Gist options
  • Save jadeapplegate/814b82b1e0855d09acb6 to your computer and use it in GitHub Desktop.
Save jadeapplegate/814b82b1e0855d09acb6 to your computer and use it in GitHub Desktop.
Introduction to Meteor.js

##What is it?

  • ####Meteor is MAGIC!!!

  • Meteor is an open source Javascript framework, written on top Node.js.

  • Meteor also comes with a useful command line tool.

  • Unlike other frameworks, Meteor has both client AND server side Javascript with built-in hosting capabilities.

  • Most of all, building sites with Meteor is fast and your code will be short!

##Core Features

The Seven Principles of Meteor

####One Language

Until now, we've built apps using javascript and a rails (or firebase) backend. Meteor allows you to write both client and server-side javascript in one app, with a file tree similar to Rails. More on this file system later...

####Full Stack Reactivity

Meteor uses .events for click events and .rendered for template events

It has built-in templating capability using mustachejs

In Meteor, you don't program routines and functions, you program page elements. It is very component-oriented, in that just instruct Meteor how something should work, and it will take care of updating the page in realtime.1

####Latency Compensation

When the user sends some information to the database, Meteor will update the browser in realtime, without waiting for the database to confirm and send that update to the browser. Without this latency compensation, the user may experience a slower response when interacting with the site.

####Packages You can think of these as the equivalent of Ruby Gems for Meteor

Add packages using $ meteor add <package_name> in the command line (and remove them using $ meteor remove <package_name>)

Examples:

User-designed Packages

Meteor also supports third-party packages. These are not part of the Meteor Core but are just as easily installed. Browse these packages on Atmosphere

Instead of $ meteor add <package_name>, run these commands:

  • Add Meteorite to your app: npm install -g meteorite
  • Add your package: mrt add <package_name>

Examples:

  • iron-router: client and server-side routing specifically for Meteor*
  • fast-render: improves the initial load time of your app and gives you 2-10 times faster page loads

Local Smart Packages

You could separate each feature of your app into packages. And it's pretty simple! Check out these instructions

##Getting started

  1. Run : $ curl https://install.meteor.com | /bin/sh, which installs a MongoDB database, a development server, and a command line program for creating and deploying your app. (Note: you only have to install this on your machine once)

  2. Create a project: $ meteor create <your_app_name>

  3. Cd into your app and run it locally: $ meteor and you will see: Meteor server running on: http://localhost:3000/, so check it out in your browser

  4. Deploy to meteor's free servers: $ meteor deploy <your_app_name>.meteor.com

#####A note about JS console Keep in mind that the eqivalent of rails c is now in your browser, so you can play with information from your database there.

#####Organization (aka: where do I put my code?)2 At first, you could just have three files: a .js file, a .css file, and a .html file. Once things get a bit more complicated, you might structure your code like this:


lib/                       # <- any common code for client/server.
lib/environment.js         # <- general configuration
lib/methods.js             # <- Meteor.method definitions
lib/external               # <- common code from someone else

**Note that js files in lib folders are loaded before other js files.

collections/               # <- definitions of collections and methods on them (could be models/)

client/lib                 # <- client specific libraries (also loaded first)
client/lib/environment.js  # <- configuration of any client side packages
client/lib/helpers         # <- any helpers (handlebars or otherwise) that are used often in view files

client/application.js      # <- subscriptions, basic Meteor.startup code.
client/index.html          # <- toplevel html
client/index.js            # <- and its JS
client/views/<page>.html   # <- the templates specific to a single page
client/views/<page>.js     # <- and the JS to hook it up
client/views/<type>/       # <- if you find you have a lot of views of the same object type
client/stylesheets/        # <- css / styl / less files

server/publications.js     # <- Meteor.publish definitions
server/lib/environment.js  # <- configuration of server side packages

public/                    # <- static files, such as images, that are served directly.

tests/                     # <- unit test files (won't be loaded on client or server)

#####Code Re-use:

Since the entire app is built in Javascript, you can write a function once and use it on both the client and server-sides. To separate them out, you can use: Meteor.isServer and Meteor.isClient, like this:

// This function is available on both the client and the server.
var greet = function(name) {
    console.log("Hi. I'm " + name);
}

// Everything in here is only run on the server.
if(Meteor.isServer) {
    greet("SERVER");
}

// Everything in here is only run on the client.
if(Meteor.isClient) {
    greet("CLIENT");
}

##Example Exercises

##Fun Things to Try

##Meteor In The Wild

##Advanced Topics

##Resources

Footnotes

  1. http://code.tutsplus.com/tutorials/whats-this-meteor-thing--net-25426

  2. http://andrewscala.com/meteor/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment