Introduction to Node.js, MongoDB & Meteor – History – What is Meteor?



Introduction to Node.js, MongoDB & Meteor – History – What is Meteor?

0 0


introduction-to-nodejs-mongodb-meteor

Introduction to Node.js, MongoDB and Meteor

On Github rajanand02 / introduction-to-nodejs-mongodb-meteor

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

Chrome V8 Engine

Node.js

What is Node.js?

It is a run time environment

Built on Chrome’s V8 engine

Building network applications on the server-side using JavaScript.

It uses event-driven and non-blocking model.

What it is not?

A Web Framework

Multi-threaded

Beginners

What you could build with Node.js?

Web socket Server

Streaming server

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/");
              
              

History

Relational Databases

Scaling

In 90's

Scaling Vertically

Scaling horizontally

Scaling horizontally

Common Characteristics of NoSQL

Schema-less

Flexible

Very very fast

Highly Scalable

Open-source

Types of NoSQL Databases

Key-Value based (Redis, Riak)

Document based (MongoDB, CouchDB)

Column-based (Hbase, Cassandra)

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?

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

7principles

1.

Data

on

wire

2.

One

language

3.

Database

everywhere

4.Latency

compensation

5.Fullstack

Reactivity

6.Embracethe

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.inGet the code.
Introduction to Node.js, MongoDB & Meteor Raj Anand -  Activist / Web Developer