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

Post on 05-Apr-2017

307 views 0 download

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

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

38 anos

Computadores Primitivos Computadores Pessoais

5

15 anos

Computadores Pessoais Internet

6

12 anos

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

20 anos

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 CasoPacktPub 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

IaaSInfrastructre as a Service

CaaSContainer/Cluster as a

Service

PaaSPlatform as a Service

SaaSSoftware as a Service

GoogleVocê

44

Nuvem

IaaSInfrastructre as a Service

On Premise PaaSPlatform as a Service

SaaSSoftware 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 - Salvandoconst 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 SkillHTTPS

81

Api de integração com a Alexaconst 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