24
Meteor Building real-time web apps Nam Ho KMS TechDay 2013

Building real time web apps with Meteor

Embed Size (px)

Citation preview

Page 1: Building real time web apps with Meteor

MeteorBuilding real-time web apps

Nam Ho KMS TechDay 2013

Page 2: Building real time web apps with Meteor

Agenda

• Play game• Meteor

Page 3: Building real time web apps with Meteor

PLAY GAME

Play game at http://starwars.meteor.com/Inspired by MMO Asteroids & Multi-touch Game Controllerhttp://seb.ly/2012/04/node-js-experiment-mmo-asteroids/http://seb.ly/2011/04/multi-touch-game-controller-in-javascripthtml5-for-ipad/

Page 4: Building real time web apps with Meteor

METEOR

A Node.JS based real-time web frameworkhttp://meteor.com/

Page 5: Building real time web apps with Meteor

Quick Start

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

$ meteor create starwars

$ cd starwars $ meteor Þ Meteor server running on: http://localhost:3000/

$ meteor deploy starwars.meteor.com

Page 6: Building real time web apps with Meteor

JAVASCRIPT END-TO-END

Page 7: Building real time web apps with Meteor

AUTOMATICALLY LOAD JAVASCRIPT FILES UP AND RUN

Page 8: Building real time web apps with Meteor

Project Structure

• root– /lib files in this folder are loaded

first– /client are only sent to the

client– /server are only run on the

server– /public static media files–main.* are loaded last

• Deeper files are loaded first

Page 9: Building real time web apps with Meteor

THE VIEW

Page 10: Building real time web apps with Meteor

Templates

<body> {{> hello}}</body>

<template name="hello"> <h1>Hello World!</h1></template>

Page 11: Building real time web apps with Meteor

Data Bindings

<template name="hello"> {{greeting}} </template>

Template.hello.greeting = “Hello World!";

Page 12: Building real time web apps with Meteor

Event Bindings

<template name="hello"> <input type="button" value="Click" /></template>

Template.hello.events({ 'click input': function() { alert(“pressed"); } });

Page 13: Building real time web apps with Meteor

REACTIVITY

Page 14: Building real time web apps with Meteor

Reactive Programming

Reactive programming consists 2 basic parts• Reactive data source• Reactive computation– A block of code contains reactive data

sources

Anytime data changes, reactive computation is automatically re-run.

Page 15: Building real time web apps with Meteor

Live View Updates

Template.hello.greeting = function() { return Session.get(‘greeting’);};

When changing Session value,view updates immediately

reactive data source

reactive computation

Page 16: Building real time web apps with Meteor

THE MODEL

Page 17: Building real time web apps with Meteor

Cached and Synced Data

Server Client

Data model Data model

Changes

#1 Cache#2 Sync

Listen

Page 18: Building real time web apps with Meteor

SYNCED MODEL+

REACTIVITY=

BANG!

Page 19: Building real time web apps with Meteor

PUBLISH / SUBSCRIBE

Publish a record set.Subscribe to a record set.

Page 20: Building real time web apps with Meteor

// server + client: create Player modelPlayers = new Meteor.Collection(“players”);

// server: publish the players collectionMeteor.publish(“allplayers”, function () { return Players.find(); });

// client: subscribe a record setMeteor.subscribe(“allplayers”);

Page 21: Building real time web apps with Meteor

SERVER METHODS

Methods are remote functions that Meteor clients can invoke.

Page 22: Building real time web apps with Meteor

// server Meteor.methods({ foo: function () { return “bar”; } });

// client: async callMeteor.call(‘foo’, function(error, result){…});

// client: sync callvar result = Meteor.call(‘foo’);

Page 23: Building real time web apps with Meteor

USE MONGODB AS DATABASE

Reset database: meteor resetShell: meteor mongo OR mongo 127.0.0.1:3002/meteor

Page 24: Building real time web apps with Meteor

THANK YOU!Q & A

Source code: https://github.com/hotrannam/starwarsTwitter: @hotrannam