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
- 8. “The more advanced we become
the faster we become at
advancing”
"Marco Annunziata: Welcome to the Age of the Industrial Internet"8
- 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
- 24. O Google Cloud Platform é construído
na mesma infraestrutura que os
serviços do google rodam
• Rede Global
• Redundância
• Infraestrutura inovadora
24
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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