Introduction to Node.js, MongoDB
& Meteor


Raj Anand -  Activist / Web Developer

Where does your JavaScript code runs?

Typically in a Browser
(client)

Jsconf 2009

Ryan Dahl

- Ryan Dahl


Chrome V8 Engine

Node.js

What is Node.js?

  1. It is a run time environment

  2. Built on Chrome’s V8 engine

  3. Building network applications on the server-side using JavaScript.

  4. It uses event-driven and non-blocking model.

What it is not?

  1. A Web Framework

  2. Multi-threaded

  3. Beginners

What you could build with Node.js?

  1. Web socket Server

  2. Streaming server

  3. JSON APIs

Blocking Vs Non-Blocking

file_read.rb
            
file = File.open('introduction.txt', 'r')

puts file.read

puts "Welcome to Summer Camp 2015"
            
            
file_read.js
            
var fs = require('fs');

fs.readFile('introduction.txt' ,'utf8', function ( err, content ){
  console.log(content);
});

console.log("Welcome to Summer Camp 2015");

Hello world in Node.js

              

var http = require('http');

var server = http.createServer(function (request, response) {

  response.writeHead(200, {"Content-Type": "text/plain"});

  response.end("Hello World\n");
});

server.listen(3000);

console.log("Server running at http://127.0.0.1:3000/");
              
              
mongodb

History

timeline timeline

Relational Databases

Scaling

scale

In 90's

Scaling Vertically

scale

Scaling horizontally

scale

Scaling horizontally

scale
scale

Common Characteristics of NoSQL

  1. Schema-less

  2. Flexible

  3. Very very fast

  4. Highly Scalable

  5. Open-source

Types of NoSQL Databases

  1. Key-Value based (Redis, Riak)

  2. Document based (MongoDB, CouchDB)

  3. Column-based (Hbase, Cassandra)

  4. Graph-based (Neo4J, OrientDB)

What is MongoDB?

Scalable, High performance, Open source, Document-oriented database.

Post Document

              
   {
       "_id": ObjectId("555cb7798440b2de2c4eef0e"),
       "user": "xyz",
       "content": "MongoDB is awesome",
       "comments": [
          {
              "user": "abc",
              "comment": "Yes, it is"
          },
         {
              "user": "efg",
              "comment": "True that."
         },
      ]
   }
              
            

Concept Mapping

Basic Commands

Crud

              
# Create
> db.post.insert( { content: "MongoDB is awesome"} )
> db.post.insert( { content: "Node.js is really cool"} )
              
            
            
# Read
> db.post.find()
> db.post.findOne( { content: "MongoDB is awesome"} )
            
          
          
# Update
> db.post.update( { content: "MongoDB is awesome" }, { $set: { content: "MongoDB is great" } } )
> db.post.update( { content: "MongoDB is great" }, { $set: { content: "MongoDB is awesome" }, { likes: 10 } } )
          
        
        
# Delete
> db.post.remove( { likes: 10 })
> db.post.remove({ })
        
      

What is Meteor?

meteor

well-known & productivity-proven

Javascript Libraries

Packaged into one

powerful platform to build modern apps

Focus on your app’s unique features instead of wrangling network code

7
principles

1.

Data

on

wire

2.

One

language

3.

Database

everywhere

4.

Latency

compensation

5.

Full
stack

Reactivity

6.

Embrace
the

Ecosystem

7.

Simplicity

equals

Productivity

Quickstart


Install Meteor:
$ curl https://install.meteor.com | /bin/sh
Create a project:
$ meteor create myapp 
Run it locally:

$ cd myapp
$ meteor
=> Meteor server running on: http://localhost:3000/

Handlebars Template


<head>
  <title>myapp</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>

  {{> hello}}
</body>

<template name="hello">
  <button>Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>
</template>

Template mangers


if (Meteor.isClient) {
  // counter starts at 0
  Session.setDefault("counter", 0);

  Template.hello.helpers({
    counter: function () {
      return Session.get("counter");
    }
  });

  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      Session.set("counter", Session.get("counter") + 1);
    }
  });
}
If (Meteor.isServer){
// Server side code 
}

Let's build something cool

Structure
&
Architecture

Folder Structure


/client/...

/server/...

/lib/...

/public/...

 main.*


LIVE HTML
&
HOT CODE PUSH

Mongo DB
&
COLLECTIONS

Synchronized Collections


Posts = new Meteor.Collection("posts");

Posts.find();
Posts.findOne();
Posts.insert();
Posts.update();
Posts.remove();
...

Publish/Subsribe


// server: publish the messages collection
Meteor.publish("messages", function () {
  return Messages.find();
}); 

// client: subscribe to the published messages
Meteor.subscribe("messages"); 

Method Calls

Meteor.methods({
  addMessage: function (message, room, username) {
    Messages.insert( {
      room: room,
      message: message,
      username: username
    } );
  }
}); 
// async call
Meteor.call('addMessage', message, room, username, function (err, res) { 
  ... 
} );

// sync call
var result = Meteor.call('addMessage', message, room, username); 

Accounts


$ meteor add accounts-ui

$ meteor add accounts-*
* = password, facebook, twitter, google, github, ...
    OAuth2

{{> loginButtons}}

Mobile Support


$ meteor add-platform * 
    
$ meteor run * 
      
* = android, ios ...
    

Deployment



on Meteor infrastructure

$ meteor deploy myapp.meteor.com


on own infrastructure

$ meteor bundle myapp.tgz

Ecosystem

ATMOSPHERE


NPM Packages

  • Over 201 groups around the world
  • More than 25k active members
  • More than 4k community packages
  • One of the most starred GitHub repo(>23k)

Learning resources

meteor.com/learn


  • Official Meteor tutorial at meteor.com/install
  • Discover Meteor
  • Documentation at docs.meteor.com
  • http://meteortips.com/book/
  • https://meteorhacks.com/

Questions?

Thank you

GitHub, Twitter - rajanand02

rajanand@fsftn.org, rajanand@hacktivist.in

Get the code.