1 of 11

Google cloud functions

Google Maps example

2 of 11

What are they ?

Node.js apps that run on a cloud VM and are invoked by a trigger

  • HTTP - post to a published address
  • Pub/Sub - publish a message
  • Storage - modify a cloud storage object
  • Direct - from the cloud console CLI

3 of 11

Where are they ?

In cloud storage

4 of 11

How are they created ?

Like any Node application, with package management & request handling.

5 of 11

How are they deployed ?

Get the source code from my repository

git pull https://github.com/brucemcpherson/effex-pd-cloudfunctions.git

Deploy it to my storage bucket as an http triggered function

gcloud beta functions deploy pdordergenerator --stage-bucket gcf_hell --trigger-http

6 of 11

How are they executed (http trigger) ?

// post a message to the cloud function

var packet = {

contents: {

item:keys.item,

message: {

updater:keys.updater,

mapsApiKey:keys.mapsApiKey

}

}

};

var response =

UrlFetchApp.fetch (url , {

payload:JSON.stringify(packet),

contentType:"application/json",

method:"post",

muteHttpExceptions:true

});

7 of 11

Google Maps in a cloud function

Node has no DOM, so you can't use regular Maps API.

But a limited version exists for Node

https://github.com/googlemaps/google-maps-services-js

npm install @google/maps

ns.maps = require('@google/maps');

ns.data.googleMapsClient = ns.maps.createClient({

key: keys.mapsApiKey,

Promise: Promise

});

8 of 11

How to test

There is a simulator that deploys and runs the function locally

npm install -g @google-cloud/functions-emulator

functions deploy pdordergenerator --trigger-http

functions call pdordergenerator

--data='{"contents":{"message":{"updater":"uxk-dela1w-b2os2bfjucqj","mapsApiKey":"AIxxxxxxxxxxxxx5YA9U"},"item":"item-pd-demo"}}'

functions logs read

9 of 11

Use as part of a workflow

A giant pub crawl generator

  • Uses an area in to limit suggestions.
  • Cloud function finds random pubs in that area at random intervals.
  • Pubs are assigned to pub crawls in Apps Script when there's enough of them
  • Pub crawl routes are displayed
  • All interactions are via push notifications in Ephemeral exchange

10 of 11

Data from cache to plot map using API

11 of 11

More info, videos and source code