Interconnected Nodes

 everywhere

Who Am I?

Alwin Arrasyid, 19 years old.

Node.js Developer at DyCode / DycodeX.

Sometimes do DevOps things.

10th gen. of RPL SMKN 1 Cimahi.


Dedicated app for movie freaks. Available for iOS, Android, and Windows Phone!

Photo collecting, management and printing solution for venues and events.

Smart Gallon

A Smart Drinking Water Monitoring Platform

Magic Printer

Your Everyday Thermal Printer. Only Smarter.

Magic Button

Personal butler. Now in your hand.

What's Node.js?

Node.js

Javascript runtime built on top of Google Chrome's V8 engine.

Node.js uses asynchronous, event-driven, non-blocking I/O.

Firstly developed by Ryan Dahl at Joyent.

Now is developed and maintained by community under control of Node.js Foundation.

So, It's javascript runtime?

Yes! The thing is, Node.js lets you run javascript code out of the browsers!

Why Should I Learn Node.js?

Or Javascript in general

Node.js is Everywhere!

From large-scale back-end, mobile apps to distributed IoT devices!

Change of "Full Stack" Terms

The "Full Stack" terms in node.js has changed from back-end and front-end to back-end, front-end and interconnected devices!

Node.js As Back-end

With those frameworks, you are able to build service-based back-end easily for your mobile apps and IoT devices!

Mobile Apps? Whoa!

In this field, Node.js acts as a platform, and build system along with other build system like gradle, etc.

Desktop Apps!

Tired of using Java or .NET to build desktop application? Try Electron!

IoT Devices!

Tools for Front-end Web

Node.js can also be used as a tool of developing front-end web app or libraries.

Honorable Mentions...

You don't need Node.js to develop Tizen app, but it uses javascript, html and css!

Why Should I Learn Node.js

Node.js is everywhere!

It uses asynchronous I/O model!

Asynchronous I/O

Node.js uses async I/O model. It doesn't block the event loop so that the next code can be executed immediately! 

 

When we call async function, we need to provide a callback function that will be executed once the async operation is finished.

// simulates user data retrieval from database
function getUser(userId, callback) {
    db.User.findById(userId, function(err, result) {
        if (err) return callback(err);
        return callback(null, result);
    });
}


// main code
console.log('Getting user data...');

getUser(1, function(err, result) {
    if (err) {
        console.error('There was an error:' + err.message);

        return false;
    }

    console.log('User data: ' + user.name);
});

console.log('done');
<?php

// db connection initialization code here....

$queryResult = mysqli_query('SELECT * FROM user WHERE id = 1');

$userData = mysqli_fetch_assoc($queryResult);

?>

Compare with this piece of PHP code

Async Workflows

What you've seen is one of some async workflows in Node.js. Actually, there's more!

  • Callback
  • Promise
  • Async/await 

Promise

Promise is an eventual value that can be returned immediately from an async function. Promise utilize then and catch method to signal result or error of an async operation .

 

Promise is chainnable! You can chain a sequence of async operations.

function getUser(id) {
    return new Promise(function(resolve, reject) {
        db.User.findById(id, function(error, result) {
            if (error) return reject(error);

            return resolve(result);
        });
    })
}

getUser(id)
    .then(function(result) {
        console.log('User: ' + result.name);
    })
    .catch(function(rejection) {
        console.error('Caught error: ' + rejection.message);
    });
function getUser(id) {
    // simulates async operation
    return Promise.resolve({ name: 'alwin', id: 1 });
}

function getUserJob(user) {
    // simulates async operation
    return Promise.resolve({
        job: 'Node.js Developer',
        user: { name: 'alwin', id: 1 }
    });
}

getUser(1)
    .then(function(result) {
        console.log('User: ' + result.name);

        return getUserJob(result);
    })
    .then(function(job) {
        console.log('Job: ' + job.job);
    })
    .catch(function(rejection) {
        console.error('Caught error: ' + rejection.message);
    });

Async/Await

Async/await makes working with promise easier!

async function getUserData() {
    var user = await getUser(1); // return Promise
    var job = await getJob(user); // return Promise, too
    
    console.log('Name: ' + user.name);
    console.log('Job: ' + job.job);
}

The code above actually asynchronous, but looks like synchronous!

Why Should I Learn Node.js

Node.js is everywhere!

It uses asynchronous I/O model!

Huge support from community & enterprises!

100,000 published modules!

source: modulus.io

By the end of 2014, there are over 100,000 modules published to npm registry!

300,000 published modules!

Announced at the Node Interactive Europe @ Amsterdam event this September.

5 million users!

Announced at the Node Interactive Europe @ Amsterdam event this September.

More Enterprises Hiring Node.js Dev!

Enterprises become a large part of the community!

That's It!

Where You Can Find Me

twitter: @alwin_wint3r

facebook: facebook.com/alwin.simple

github: github.com/alwint3r

email: alwin.ridd@gmail.com

            winter@dycode.com

 

DyPlex: Jl. Setrasari Kulon II No. 2

              (Mon - Fri, 9 AM - 6 PM)

Image Sources

commons.wikimedia.org

microsoft.com

developer.tizen.org

github.com

seeedstudio.com

getchip.com/pages/store

esp8266.com

espruino.com

beagleboard.org

raspberrypi.org

ionicframework.com

cordova.apache.org

krakenjs.com

facebook.github.com

sailsjs.org

feathersjs.com

dycode.co.id

dycodex.com

Interconnected Nodes

By Alwin Arrasyid

Interconnected Nodes

  • 980