SlideShare a Scribd company logo
Backend, App e Internet das Coisas
com NodeJS e
Google Cloud Platform
Alvaro Viebrantz
aviebrantz.com.br
@alvaroviebrantz
medium.com/iot-bootcamp
1
Alvaro Viebrantz
DevMT e GDGCuiabá
Fullstack developer
aviebrantz.com.br // @alvaroviebrantz
medium.com/iot-bootcamp
2
Agenda
• O que é computação em nuvem.
• Google Cloud Platform Overview.
• NodeJS
• Estudo de Caso - PacktPub Notifier
• Backend - Google App Engine
• App - React Native + Firebase
• IoT - Alexa Custom Skill
3
4
38anos
Computadores Primitivos Computadores Pessoais
5
15anos
Computadores Pessoais Internet
6
12anos
Internet Smartphones / Nuvem / Mobile
7
“The more advanced we become
the faster we become at
advancing”
"Marco Annunziata: Welcome to the Age of the Industrial Internet"8
9
10
4.7
bilhões de
página
A Web é gigante hoje
Era do
Zetabyte*
* 1000 Exabytes
36.000
anos
de video
em hd
Últimos
20anos
http://www.livescience.com/54094-how-big-is-the-internet.html
11
Nossa noção de sucesso mudou…
12
13
14
http://press.spotify.com/us/category/pictures/
15
16
Como isso é possível ?
17
18
19
Pilares
Disponibilidade Manutenção Escalável
Economia
20
Escalabilidade
Horizontal
Escalabilidade
VerticalX
21
22
Porque Google Cloud Platform ?
23
O Google Cloud Platform é construído
na mesma infraestrutura que os
serviços do google rodam
• Rede Global
• Redundância
• Infraestrutura inovadora
24
25
Regiões
26
Vantagens
Preço
(Cobrança por minuto)
Maquinas Customizáveis

(até GPU)
Facilmente Escalável
Developer Experience ❤
27
28
29
Estudo de Caso
30
Estudo de Caso
PacktPub Notifier
Backend
App
IoT
31
32
Node.js
• Engine V8 do Google Chrome
• Rápido e escalável
• Orientado a eventos e não bloqueante
• Muito leve
• Perfeito para aplicações real-time
33
É apenas Javascript
• Navegador
• Server side
• Desktop - Electron e NW
• Mobile - Cordova, Ionic, React Native, etc.
• Embarcado - Mongoose OS e Espruino
• Geladeira, torradeira, etc
34
Ecossistema rico
• Muitas ferramentas feitas com Node
• Webpack ❤
• Gerenciadores de pacotes
• NPM e Yarn
• Editores
• Visual Studio Code, Atom, Webstorm
• Mais de 450 mil pacotes no npmjs.com
35
Web Frameworks
• Express.js
• Hapi.js
• Koa
36
Stack escolhido
• Hapi.js no Backend
• Aplicativos iOS e Android nativos com React Native
• Vários serviços do GCP
• Database, Cron, Tasks, HTTPS, etc
37
PacktPub Notifier v0.1
iOS e
Android
Packt
Publishing
Website
Backend
38
Hello World com HapiJS
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({
host: '0.0.0.0',
port: process.env.PORT || 8080
});
server.route({
method: 'GET',
path: '/api/tasks/fetch-books',
config: {
handler: (request, reply) => {
reply({ message: 'Hello
World'})
}
}
});
server.start((err) => {
if (err) {
throw err;
}
});
39
Web Scrapping
'use strict';
const cheerio = require('cheerio');
const fetch = require('node-fetch');
const urlFreeLearning = 'http://www.packtpub.com/packt/offers/free-learning/';
class PackPubCrawler {
/* Código omitido */
async fetchBooksFromPacktPub() {
let body = await this.fetchPageBody();
let documentSelector = cheerio.load(body);
let currentBook = this.scrapeCurrentBook(documentSelector);
return {
currentBook
};
}
}
module.exports = PackPubCrawler;
40
Api de Livros
'use strict';
const Boom = require('boom');
const PackPubCrawler = require('../PacktPubCrawler');
module.exports = {
method: 'GET',
path: '/api/books',
config: {
handler: async (request, reply) => {
try {
const crawler = new PackPubCrawler();
let books = await crawler.fetchBooksFromPacktPub();
console.log('Success Fetching Books');
reply({ books });
} catch (e) {
console.log('Error Fetching Books', e);
reply(Boom.badGateway('Xablau', e))
}
}
}
}
41
Api de Livros
42
43
Nuvem
IaaS

Infrastructre as a Service
CaaS

Container/Cluster as a
Service
PaaS

Platform as a Service
SaaS

Software as a Service
GoogleVocê
44
Nuvem
IaaS

Infrastructre as a Service
On Premise PaaS

Platform as a Service
SaaS

Software as a Service
• Aplicação
• Dados
• Runtime
• Middleware
• SO
• Virtualização
• Servidores
• Storage
• Redes
• Aplicação
• Dados
• Runtime
• Middleware
• SO
• Virtualização
• Servidores
• Storage
• Redes
• Aplicação
• Dados
• Runtime
• Middleware
• SO
• Virtualização
• Servidores
• Storage
• Redes
• Aplicação
• Dados
• Runtime
• Middleware
• SO
• Virtualização
• Servidores
• Storage
• Redes
Gerenciado por você
Gerenciado magicamente45
Google App Engine - Standard vs Flexible
46
47
48
https://googlecloudplatform.github.io/google-cloud-node
49
1. Instale o Cloud SDK (cli)
2. Configure o app.yaml
3. cloud app deploy
https://cloud.google.com/sdk/
runtime: nodejs
env: flex
skip_files:
- ^node_modules$
resources:
cpu: .5
memory_gb: .6
disk_size_gb: 10
manual_scaling:
instances: 1
#automatic_scaling:
# min_num_instances: 1
# max_num_instances: 1
# cool_down_period_sec: 120
# cpu_utilization:
# target_utilization: 0.8
50
PacktPub Notifier v0.1 - Fail
51
Really slow rest api
52
PacktPub Notifier v0.2
iOS e
Android
Packt
Publishing
Website
Storage

Database
53
We need a database
54
NoSQL escolhido - Flexibilidade
55
Serviço de Livros - Salvando
const Datastore = require('@google-cloud/datastore');
// Instantiates a client
const datastore = Datastore({
projectId: config.projectId
});
const kind = 'Book';
class BookService {
/* Codigo omitido */
async save(book) {
let slug = this.getSlug(book);
book.slug = slug;
const bookKey = datastore.key([kind, slug]);
let bookEntity = {
key: bookKey,
data: book
};
return datastore.save(bookEntity);
}
}
56
Serviço de Livros - Consulta
// The kind for the new entity
const kind = 'Book';
class BookService {
/* Codigo omitido */
async all() {
const query = datastore.createQuery(kind)
.order('date', { descending: true });
let results = await datastore.runQuery(query);
return results[0];
}
}
57
API de tarefa de busca e armazenamento de livros
module.exports = {
method: 'GET',
path: '/api/tasks/fetch-books',
config: {
handler: async (request, reply) => {
const crawler = new PackPubCrawler();
let books = await crawler.fetchBooksFromPacktPub();
const service = new BooksService();
let slug = service.getSlug(books.currentBook);
let exists = await service.exists(slug);
if (!exists) {
// Save new book
await service.save(books.currentBook);
//TODO: Notify clients that subscribed to this
}
reply({ books });
}
}
}
58
API de Livros
const BooksService = require('../BooksService');
module.exports = {
method: 'GET',
path: '/api/books',
config: {
handler: async (request, reply) => {
let service = new BooksService();
let books = await service.all();
reply({ books });
}
}
}
59
API de Livros
60
Datastore console
61
PacktPub Notifier v0.2
iOS e
Android
Packt
Publishing
Website
62
Agendamento de tarefas
Como fazer em ambiente
distribuido ?
63
Agendamento de tarefas
64
Agendamento de tarefas - Gambiarra
65
1. Sintaxe do unix cron ou mais natural
2. Configure um cron.yaml
3. cloud app deploy cron.yaml
Google App Engine - cron.yaml
cron:
- description: fetch book every 30 mins
url: /api/tasks/fetch-books
schedule: every 30 mins
target: default
66
Google App Engine - cron.yaml
67
Google App Engine - Logs, monitoramento e trace
1. Pacote @google/cloud-trace
2. Just works
if (process.env.NODE_ENV === 'production') {
require('@google/cloud-trace').start();
}
68
Google App Engine - Logs, monitoramento e trace
69
Google App Engine - Logs, monitoramento e trace
70
PacktPub Notifier v0.5
iOS e
Android
Packt
Publishing
Website
71
Push Notifications
72%
Abrem o app quando
recebe uma notificação
72
Fluxo Push Notifications
Android iOS
Backend
GCM APNS
Device
Token
73
74
75
const admin = require("firebase-admin");
let credential = admin.credential.applicationDefault();
admin.initializeApp({
credential,
databaseURL: "https://iot-bootcamp-158521.firebaseio.com"
});
const messaging = admin.messaging();
const TOPIC_NAME = 'receive_book_notification';
class BookNotificationService {
async notifyAllClients(book) {
var payload = {
notification: {
title: `${book.title} is free today!`,
body: `Open the app to claim this book.`
}
};
return messaging.sendToTopic(TOPIC_NAME, payload);
}
}
Firebase Admin SDK
76
Client Side - React Native
import Firestack from 'react-native-firestack';
import FCM from 'react-native-fcm';
const TOPIC_NAME = 'receive_book_notification';
async subscribe() {
let { wantsToReceiveNotifications } = this.state;
if (!wantsToReceiveNotifications) {
FCM.requestPermissions();
FCM.subscribeToTopic(TOPIC_NAME);
firestack.analytics.logEventWithName('subscribe', {});
} else {
FCM.unsubscribeFromTopic(TOPIC_NAME);
firestack.analytics.logEventWithName('unsubscribe', {});
}
this.setState({ wantsToReceiveNotifications: !
wantsToReceiveNotifications })
}
77
Firebase Analytics e Push
78
iOS e
Android
PacktPub Notifier v0.8
Packt
Publishing
Website
79
80
Fluxo Alexa Custom Skill
Backend
Usuário
Alexa, Ask Packt Publishing
Fan what’s the book of the day
The free book of the day is title
Learning Raspberry Pi
Alexa Skill
HTTPS
81
Api de integração com a Alexa
const BooksService = require('../BooksService');
module.exports = {
method: 'POST',
path: '/api/alexa',
config: {
handler: async (request, reply) => {
let service = new BooksService();
let book = await service.getLastBook();
let message = `The free book of the day is titled $
{book.title}`;
reply({
"version": "1.0",
"sessionAttributes": {},
"response": {
"shouldEndSession": true,
"outputSpeech": {
"type": "SSML",
"ssml": `<speak>${message}</speak>`
},
}
});
}
}
} 82
PacktPub Notifier - Final
iOS e
Android
Amazon
Echo
Alexa
Custom
Skill
Packt
Publishing
Website
83
Futuro ?
84
• Funções escritas em NodeJS
• Escalabilidade automatica
• Serverless
• Podem ser chamadas por eventos
• Mudanças de arquivo, banco, http, etc.
85
Serverless
86
Duvidas ?
Alvaro Viebrantz
aviebrantz.com.br
@alvaroviebrantz
medium.com/iot-bootcamp
https://github.com/alvarowolfx/packtpub-notifier-gcp
87
Links úteis
• https://cloud.google.com/sdk/
• https://developers.google.com
• https://cloud.google.com/products/
• https://firebase.google.com
• https://nodejs.org/en/
• https://developer.amazon.com
• https://facebook.github.io/react-native/
88
Referência
• https://pt.slideshare.net/FrancescoMarchitelli1/google-cloud-platform-47074110
• https://www.infoq.com/br/presentations/plataforma-digital-com-google-cloud-
platform
• https://cloud.google.com/icons/
89

More Related Content

PPTX
LiveOps para games usando o Firebase
PDF
Google App Engine for Java v0.0.2
PPTX
How to make your Money Machine with Internet of Things
DOCX
GOOGLE APP ENGINE Training in Chennai
PPTX
Are you ready for cloud-native java JavaCro2019
PPTX
Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...
PDF
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
PPTX
Physical web
LiveOps para games usando o Firebase
Google App Engine for Java v0.0.2
How to make your Money Machine with Internet of Things
GOOGLE APP ENGINE Training in Chennai
Are you ready for cloud-native java JavaCro2019
Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Physical web

Viewers also liked (20)

PDF
Introdução a Machine Learning e TensorFlow
PDF
Introdução a Machine Learning e TensorFlow
PDF
Go-jek's Go-Food Chatbot
PPTX
El currículum de mi vida
PPTX
Growing Makers in Medicine, Life Sciences, and Healthcare
PPTX
Iron Values TPC, Spare 3, part 1/2
PDF
Scrum! But ... SAP Inside Track Frankfurt 2017
PPTX
Игорь Фесенко "What’s New in C# 7.0"
PDF
Using Onyx in anger
PDF
Packet optical transformation ofc2017
PDF
JLL's City Momentum Index 2017
 
PPTX
Alfalfa homoeopathic materia medica slide show presentation by Dr. Hansaraj s...
ODT
10 bài
PDF
Ross broiler handbook
PDF
Doha courses plan 2017
PDF
Angular of things: angular2 + web bluetooth
PDF
Wall Street Mobile Technologies Conference, Bank's "Uber Moment" and Open Ban...
PDF
Меню EverJazz
DOCX
The greatest tragedy of western front pakistani stupidity at its lowest height
PPS
Gustavo Germano Proyecto Ausencias
Introdução a Machine Learning e TensorFlow
Introdução a Machine Learning e TensorFlow
Go-jek's Go-Food Chatbot
El currículum de mi vida
Growing Makers in Medicine, Life Sciences, and Healthcare
Iron Values TPC, Spare 3, part 1/2
Scrum! But ... SAP Inside Track Frankfurt 2017
Игорь Фесенко "What’s New in C# 7.0"
Using Onyx in anger
Packet optical transformation ofc2017
JLL's City Momentum Index 2017
 
Alfalfa homoeopathic materia medica slide show presentation by Dr. Hansaraj s...
10 bài
Ross broiler handbook
Doha courses plan 2017
Angular of things: angular2 + web bluetooth
Wall Street Mobile Technologies Conference, Bank's "Uber Moment" and Open Ban...
Меню EverJazz
The greatest tragedy of western front pakistani stupidity at its lowest height
Gustavo Germano Proyecto Ausencias
Ad

Similar to Backend, app e internet das coisas com NodeJS no Google Cloud Platform (20)

PPTX
CloudPlatforms-Cloud PLatforms evaluation
PDF
GCP overview
PDF
Up and Running with firebase
PPTX
Feed Herny developer training : crossplatform and HTML5
PPTX
Powering your Apps via Google Cloud Platform
PPTX
Using Google App Engine Python
PPTX
Codestrong 2012 breakout session the role of cloud services in your next ge...
PDF
Entrepreneurship Tips With HTML5 & App Engine Startup Weekend (June 2012)
PPTX
Advance Mobile Application Development class 04
PDF
Mobile App Development Trends in 2021
PPTX
Introduction to Google Cloud Services / Platforms
PPTX
Not a Kubernetes fan? The state of PaaS in 2025
PDF
Modern Web Cloud Architecture based on Google Technologies
PDF
Google Cloud Endpoints: Building Third-Party APIs on Google AppEngine
PPTX
Herramientas para sacar el mayor rendimiento de tu app por Google
PPTX
Simplifying Enterprise Mobility - Powering Mobile Apps from The Cloud
PDF
Rapid AI Development with Firebase Studio and Gemini Code Assist - Umar Faruq...
PDF
Test Centre case studies - Cathal McGloin (FeedHenry)
PPTX
PaaS + Appcelerator = WIN
PDF
Building Scalable Apps With Redis And Nodejs Joshua Johanan
CloudPlatforms-Cloud PLatforms evaluation
GCP overview
Up and Running with firebase
Feed Herny developer training : crossplatform and HTML5
Powering your Apps via Google Cloud Platform
Using Google App Engine Python
Codestrong 2012 breakout session the role of cloud services in your next ge...
Entrepreneurship Tips With HTML5 & App Engine Startup Weekend (June 2012)
Advance Mobile Application Development class 04
Mobile App Development Trends in 2021
Introduction to Google Cloud Services / Platforms
Not a Kubernetes fan? The state of PaaS in 2025
Modern Web Cloud Architecture based on Google Technologies
Google Cloud Endpoints: Building Third-Party APIs on Google AppEngine
Herramientas para sacar el mayor rendimiento de tu app por Google
Simplifying Enterprise Mobility - Powering Mobile Apps from The Cloud
Rapid AI Development with Firebase Studio and Gemini Code Assist - Umar Faruq...
Test Centre case studies - Cathal McGloin (FeedHenry)
PaaS + Appcelerator = WIN
Building Scalable Apps With Redis And Nodejs Joshua Johanan
Ad

More from Alvaro Viebrantz (20)

PDF
Construindo Apps/Projetos com Generative AI
PDF
Minha jornada na ciência da computação- do diploma ao Google
PDF
BigQuery Performance Improvements Storage API
PDF
End to End IoT projects with Zephyr.pdf
PDF
Carreira de Desenvolvimento
PDF
Construindo aplicações Cloud Native em Go
PDF
Prototipação em hackathons
PDF
Building REST APIs using gRPC and Go
PDF
TinyML - IoT e Machine Learning
PDF
O que projetos de IoT precisam ?
PDF
Ambiente de CI/CD com Google Cloud
PDF
Big Query - Escalabilidade Infinita para os seus Dados
PDF
Rodando uma API Com Django Rest Framework no Google Cloud
PDF
Edge computing na prática com IoT, Machine Learning e Google Cloud
PDF
Edge computing in practice using IoT, Tensorflow and Google Cloud
PDF
Iniciando com LoRa, The Things Network e Google Cloud
PDF
Construindo projetos para o Google Assistant - I/O 2019 Recap São Paulo
PDF
Edge computing na prática com IoT, Machine Learning e Google Cloud
PDF
Construindo projetos com Google Assistant e IoT
PDF
Explorando Go em Ambiente Embarcado
Construindo Apps/Projetos com Generative AI
Minha jornada na ciência da computação- do diploma ao Google
BigQuery Performance Improvements Storage API
End to End IoT projects with Zephyr.pdf
Carreira de Desenvolvimento
Construindo aplicações Cloud Native em Go
Prototipação em hackathons
Building REST APIs using gRPC and Go
TinyML - IoT e Machine Learning
O que projetos de IoT precisam ?
Ambiente de CI/CD com Google Cloud
Big Query - Escalabilidade Infinita para os seus Dados
Rodando uma API Com Django Rest Framework no Google Cloud
Edge computing na prática com IoT, Machine Learning e Google Cloud
Edge computing in practice using IoT, Tensorflow and Google Cloud
Iniciando com LoRa, The Things Network e Google Cloud
Construindo projetos para o Google Assistant - I/O 2019 Recap São Paulo
Edge computing na prática com IoT, Machine Learning e Google Cloud
Construindo projetos com Google Assistant e IoT
Explorando Go em Ambiente Embarcado

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
Teaching material agriculture food technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Electronic commerce courselecture one. Pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
cuic standard and advanced reporting.pdf
PPTX
Cloud computing and distributed systems.
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Encapsulation theory and applications.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Understanding_Digital_Forensics_Presentation.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Teaching material agriculture food technology
The AUB Centre for AI in Media Proposal.docx
Electronic commerce courselecture one. Pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
cuic standard and advanced reporting.pdf
Cloud computing and distributed systems.
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation_ Review paper, used for researhc scholars
Encapsulation theory and applications.pdf
Review of recent advances in non-invasive hemoglobin estimation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The Rise and Fall of 3GPP – Time for a Sabbatical?

Backend, app e internet das coisas com NodeJS no Google Cloud Platform

  • 1. Backend, App e Internet das Coisas com NodeJS e Google Cloud Platform Alvaro Viebrantz aviebrantz.com.br @alvaroviebrantz medium.com/iot-bootcamp 1
  • 2. Alvaro Viebrantz DevMT e GDGCuiabá Fullstack developer aviebrantz.com.br // @alvaroviebrantz medium.com/iot-bootcamp 2
  • 3. Agenda • O que é computação em nuvem. • Google Cloud Platform Overview. • NodeJS • Estudo de Caso - PacktPub Notifier • Backend - Google App Engine • App - React Native + Firebase • IoT - Alexa Custom Skill 3
  • 4. 4
  • 7. 12anos Internet Smartphones / Nuvem / Mobile 7
  • 8. “The more advanced we become the faster we become at advancing” "Marco Annunziata: Welcome to the Age of the Industrial Internet"8
  • 9. 9
  • 10. 10
  • 11. 4.7 bilhões de página A Web é gigante hoje Era do Zetabyte* * 1000 Exabytes 36.000 anos de video em hd Últimos 20anos http://www.livescience.com/54094-how-big-is-the-internet.html 11
  • 12. Nossa noção de sucesso mudou… 12
  • 13. 13
  • 14. 14
  • 16. 16
  • 17. Como isso é possível ? 17
  • 18. 18
  • 19. 19
  • 22. 22
  • 23. Porque Google Cloud Platform ? 23
  • 24. O Google Cloud Platform é construído na mesma infraestrutura que os serviços do google rodam • Rede Global • Redundância • Infraestrutura inovadora 24
  • 25. 25
  • 27. Vantagens Preço (Cobrança por minuto) Maquinas Customizáveis
 (até GPU) Facilmente Escalável Developer Experience ❤ 27
  • 28. 28
  • 29. 29
  • 31. Estudo de Caso PacktPub Notifier Backend App IoT 31
  • 32. 32
  • 33. Node.js • Engine V8 do Google Chrome • Rápido e escalável • Orientado a eventos e não bloqueante • Muito leve • Perfeito para aplicações real-time 33
  • 34. É apenas Javascript • Navegador • Server side • Desktop - Electron e NW • Mobile - Cordova, Ionic, React Native, etc. • Embarcado - Mongoose OS e Espruino • Geladeira, torradeira, etc 34
  • 35. Ecossistema rico • Muitas ferramentas feitas com Node • Webpack ❤ • Gerenciadores de pacotes • NPM e Yarn • Editores • Visual Studio Code, Atom, Webstorm • Mais de 450 mil pacotes no npmjs.com 35
  • 36. Web Frameworks • Express.js • Hapi.js • Koa 36
  • 37. Stack escolhido • Hapi.js no Backend • Aplicativos iOS e Android nativos com React Native • Vários serviços do GCP • Database, Cron, Tasks, HTTPS, etc 37
  • 38. PacktPub Notifier v0.1 iOS e Android Packt Publishing Website Backend 38
  • 39. Hello World com HapiJS const Hapi = require('hapi'); const server = new Hapi.Server(); server.connection({ host: '0.0.0.0', port: process.env.PORT || 8080 }); server.route({ method: 'GET', path: '/api/tasks/fetch-books', config: { handler: (request, reply) => { reply({ message: 'Hello World'}) } } }); server.start((err) => { if (err) { throw err; } }); 39
  • 40. Web Scrapping 'use strict'; const cheerio = require('cheerio'); const fetch = require('node-fetch'); const urlFreeLearning = 'http://www.packtpub.com/packt/offers/free-learning/'; class PackPubCrawler { /* Código omitido */ async fetchBooksFromPacktPub() { let body = await this.fetchPageBody(); let documentSelector = cheerio.load(body); let currentBook = this.scrapeCurrentBook(documentSelector); return { currentBook }; } } module.exports = PackPubCrawler; 40
  • 41. Api de Livros 'use strict'; const Boom = require('boom'); const PackPubCrawler = require('../PacktPubCrawler'); module.exports = { method: 'GET', path: '/api/books', config: { handler: async (request, reply) => { try { const crawler = new PackPubCrawler(); let books = await crawler.fetchBooksFromPacktPub(); console.log('Success Fetching Books'); reply({ books }); } catch (e) { console.log('Error Fetching Books', e); reply(Boom.badGateway('Xablau', e)) } } } } 41
  • 43. 43
  • 44. Nuvem IaaS
 Infrastructre as a Service CaaS
 Container/Cluster as a Service PaaS
 Platform as a Service SaaS
 Software as a Service GoogleVocê 44
  • 45. Nuvem IaaS
 Infrastructre as a Service On Premise PaaS
 Platform as a Service SaaS
 Software as a Service • Aplicação • Dados • Runtime • Middleware • SO • Virtualização • Servidores • Storage • Redes • Aplicação • Dados • Runtime • Middleware • SO • Virtualização • Servidores • Storage • Redes • Aplicação • Dados • Runtime • Middleware • SO • Virtualização • Servidores • Storage • Redes • Aplicação • Dados • Runtime • Middleware • SO • Virtualização • Servidores • Storage • Redes Gerenciado por você Gerenciado magicamente45
  • 46. Google App Engine - Standard vs Flexible 46
  • 47. 47
  • 48. 48
  • 50. 1. Instale o Cloud SDK (cli) 2. Configure o app.yaml 3. cloud app deploy https://cloud.google.com/sdk/ runtime: nodejs env: flex skip_files: - ^node_modules$ resources: cpu: .5 memory_gb: .6 disk_size_gb: 10 manual_scaling: instances: 1 #automatic_scaling: # min_num_instances: 1 # max_num_instances: 1 # cool_down_period_sec: 120 # cpu_utilization: # target_utilization: 0.8 50
  • 53. PacktPub Notifier v0.2 iOS e Android Packt Publishing Website Storage
 Database 53
  • 54. We need a database 54
  • 55. NoSQL escolhido - Flexibilidade 55
  • 56. Serviço de Livros - Salvando const Datastore = require('@google-cloud/datastore'); // Instantiates a client const datastore = Datastore({ projectId: config.projectId }); const kind = 'Book'; class BookService { /* Codigo omitido */ async save(book) { let slug = this.getSlug(book); book.slug = slug; const bookKey = datastore.key([kind, slug]); let bookEntity = { key: bookKey, data: book }; return datastore.save(bookEntity); } } 56
  • 57. Serviço de Livros - Consulta // The kind for the new entity const kind = 'Book'; class BookService { /* Codigo omitido */ async all() { const query = datastore.createQuery(kind) .order('date', { descending: true }); let results = await datastore.runQuery(query); return results[0]; } } 57
  • 58. API de tarefa de busca e armazenamento de livros module.exports = { method: 'GET', path: '/api/tasks/fetch-books', config: { handler: async (request, reply) => { const crawler = new PackPubCrawler(); let books = await crawler.fetchBooksFromPacktPub(); const service = new BooksService(); let slug = service.getSlug(books.currentBook); let exists = await service.exists(slug); if (!exists) { // Save new book await service.save(books.currentBook); //TODO: Notify clients that subscribed to this } reply({ books }); } } } 58
  • 59. API de Livros const BooksService = require('../BooksService'); module.exports = { method: 'GET', path: '/api/books', config: { handler: async (request, reply) => { let service = new BooksService(); let books = await service.all(); reply({ books }); } } } 59
  • 62. PacktPub Notifier v0.2 iOS e Android Packt Publishing Website 62
  • 63. Agendamento de tarefas Como fazer em ambiente distribuido ? 63
  • 65. Agendamento de tarefas - Gambiarra 65
  • 66. 1. Sintaxe do unix cron ou mais natural 2. Configure um cron.yaml 3. cloud app deploy cron.yaml Google App Engine - cron.yaml cron: - description: fetch book every 30 mins url: /api/tasks/fetch-books schedule: every 30 mins target: default 66
  • 67. Google App Engine - cron.yaml 67
  • 68. Google App Engine - Logs, monitoramento e trace 1. Pacote @google/cloud-trace 2. Just works if (process.env.NODE_ENV === 'production') { require('@google/cloud-trace').start(); } 68
  • 69. Google App Engine - Logs, monitoramento e trace 69
  • 70. Google App Engine - Logs, monitoramento e trace 70
  • 71. PacktPub Notifier v0.5 iOS e Android Packt Publishing Website 71
  • 72. Push Notifications 72% Abrem o app quando recebe uma notificação 72
  • 73. Fluxo Push Notifications Android iOS Backend GCM APNS Device Token 73
  • 74. 74
  • 75. 75
  • 76. const admin = require("firebase-admin"); let credential = admin.credential.applicationDefault(); admin.initializeApp({ credential, databaseURL: "https://iot-bootcamp-158521.firebaseio.com" }); const messaging = admin.messaging(); const TOPIC_NAME = 'receive_book_notification'; class BookNotificationService { async notifyAllClients(book) { var payload = { notification: { title: `${book.title} is free today!`, body: `Open the app to claim this book.` } }; return messaging.sendToTopic(TOPIC_NAME, payload); } } Firebase Admin SDK 76
  • 77. Client Side - React Native import Firestack from 'react-native-firestack'; import FCM from 'react-native-fcm'; const TOPIC_NAME = 'receive_book_notification'; async subscribe() { let { wantsToReceiveNotifications } = this.state; if (!wantsToReceiveNotifications) { FCM.requestPermissions(); FCM.subscribeToTopic(TOPIC_NAME); firestack.analytics.logEventWithName('subscribe', {}); } else { FCM.unsubscribeFromTopic(TOPIC_NAME); firestack.analytics.logEventWithName('unsubscribe', {}); } this.setState({ wantsToReceiveNotifications: ! wantsToReceiveNotifications }) } 77
  • 79. iOS e Android PacktPub Notifier v0.8 Packt Publishing Website 79
  • 80. 80
  • 81. Fluxo Alexa Custom Skill Backend Usuário Alexa, Ask Packt Publishing Fan what’s the book of the day The free book of the day is title Learning Raspberry Pi Alexa Skill HTTPS 81
  • 82. Api de integração com a Alexa const BooksService = require('../BooksService'); module.exports = { method: 'POST', path: '/api/alexa', config: { handler: async (request, reply) => { let service = new BooksService(); let book = await service.getLastBook(); let message = `The free book of the day is titled $ {book.title}`; reply({ "version": "1.0", "sessionAttributes": {}, "response": { "shouldEndSession": true, "outputSpeech": { "type": "SSML", "ssml": `<speak>${message}</speak>` }, } }); } } } 82
  • 83. PacktPub Notifier - Final iOS e Android Amazon Echo Alexa Custom Skill Packt Publishing Website 83
  • 85. • Funções escritas em NodeJS • Escalabilidade automatica • Serverless • Podem ser chamadas por eventos • Mudanças de arquivo, banco, http, etc. 85
  • 88. Links úteis • https://cloud.google.com/sdk/ • https://developers.google.com • https://cloud.google.com/products/ • https://firebase.google.com • https://nodejs.org/en/ • https://developer.amazon.com • https://facebook.github.io/react-native/ 88