SlideShare a Scribd company logo
1 of 25
Google Cloud Functions:
try { Kotlin } insteadOf JavaScript
How to write (almost) free backend services on
Google Cloud Functions in Kotlin
Omar Miatello
Android Developer @
Organizer @
Personal profile
google.com/+OmarMiatello
Slides available on:
Google Presentation
https://goo.gl/CMVNqS
Google Photo
https://goo.gl/HpRLw4
Speaker Deck (by GitHub)
https://speakerdeck.com/jacklt
SlideShare (by LinkedIn)
https://www.slideshare.net/OmarMiatello
I’m an Android developer (since 2010)
About me
I’m an Android developer (since 2010)
I use Kotlin (since 2015) instead of Java
About me
I’m an Android developer (since 2010)
I use Kotlin (since 2015) instead of Java
I use a lot of Firebase services
About me
Target: cost-free backend!
Free hosting options:
● PHP (a lot of free hosting, since 2000)
○ Altervista, Netsons, …
● Solution PaaS (Platform as a Service)
○ Google App Engine
■ Support for: Node.js, Java, Ruby, C#, Go, Python e PHP, …
■ 28 hours instance a day
○ Amazon Web Services (Free tear only for the first 12 months)
● Solution “serverless”
Target: cost-free backend!
Comparison free "serverless" plans:
● Google Cloud Functions
○ Node.js
○ 2.000.000 free calls/month
● Cloud Functions for Firebase
○ Node.js
○ 125.000 / 2.000.000 free calls/month
● AWS Lambda
○ Node.js, Java, C#, Go, Python
○ 1.000.000 free calls/month
2.000.000
calls/month?
What if:
I call a function
every second
1 day = 86.400 seconds
~ 12 days = 1.000.000 seconds
~ 23 days = 2.000.000 seconds
30 days = 2.592.000 seconds
2.000.000
calls/month?
How many users?
1 day = ~ 65.000 calls
depends on Average User Activity
100 calls/day = ~ 650 users/day
10 calls/day = ~ 6.500 users/day
Setup: Cloud Functions + Kotlin
(Node.js)
+
Kotlin for JavaScript
picture: https://blog.jetbrains.com/kotlin/2017/11/kotlin-1-2-released
Setup: Cloud Functions + Kotlin
(Node.js)
+
Kotlin for JavaScript
picture: https://blog.jetbrains.com/kotlin/2017/11/kotlin-1-2-released
New Project in Kotlin/JS
● Download: IntelliJ IDEA Community Edition (Free)
○ https://www.jetbrains.com/idea/download
● New Project: Gradle > Kotlin (JavaScript)
New Project in Kotlin/JS
● Download: IntelliJ IDEA Community Edition (Free)
○ https://www.jetbrains.com/idea/download
● New Project: Gradle > Kotlin (JavaScript)
● GroupId: “com.example” & ArtifactId: “myservice”
● Confirm default settings until “Finish”
Configure: Cloud Functions for Firebase
● Create new project on Firebase (optional)
○ Use console: https://console.firebase.google.com
Configure: Cloud Functions for Firebase
● Create new project on Firebase (optional)
○ Use console: https://console.firebase.google.com
● Open Functions section > click “Start”
Configure: Cloud Functions for Firebase
● Create new project on Firebase (optional)
○ Use console: https://console.firebase.google.com
● Open Functions section > click “Start”
● Install Node.js: https://nodejs.org
○ Command line: “npm install -g firebase-tools” and “npm install kotlin”
○ Open Terminal in project folder and run “firebase init” (and follow instructions)
Configure: Gradle
● Add these lines in build.gradle
● Ready!
○ Deploy command: firebase deploy
compileKotlin2Js.kotlinOptions {
moduleKind = "commonjs"
outputFile = "functions/index.js"
}
Kotlin for JavaScript
JavaScript is a dynamically-typed language, which means it does not check
types at compile-time.
You can freely talk to JavaScript from Kotlin via dynamic types, but if you want
the full power of Kotlin type system, you can create Kotlin headers for JavaScript
libraries.
Kotlin: useful keywords for JavaScript
Keywords:
● external - Modifier to tell Kotlin that a certain declaration is written in pure
JavaScript. Compiler assumes that the implementation for the corresponding
class, function or property is provided by the developer.
● dynamic - The dynamic type basically turns off Kotlin's type checker. The
most peculiar feature of dynamic is that we are allowed to call any property
or function with any parameters on a dynamic variable.
Kotlin: useful functions for JavaScript
Extensions:
● .asDynamic() - Reinterprets this value as a value of the dynamic type.
● .unsafeCast<T>() - Reinterprets this “dynamic” value as a value of the
specified type [T] (ex: String / Array) without any actual type checking.
Methods:
● js(code) - Puts the given piece of a JavaScript code right into the calling
function. The compiler replaces call to `js(...)` code with the string constant
provided as a parameter.
Example: Utils.kt
fun toJson(obj: dynamic) = JSON.stringify(obj) { key, value -> value ?: undefined }
// Arrays / Map utils
fun jsMap(init: (dynamic) -> Unit) = Any().apply { init(asDynamic()) }.asDynamic()
fun keys(obj: dynamic) = js("Object").keys(obj).unsafeCast<Array<String>>()
// JavaScript functions
fun getDateOfWeek(week: Int, year: Int) = js("""
var simple = new Date(year, 0, 1 + (week - 1) * 7);
var dow = simple.getDay();
var iso = simple;
if (dow <= 4) iso.setDate(simple.getDate() - simple.getDay() + 1);
else iso.setDate(simple.getDate() + 8 - simple.getDay());
return iso;""").unsafeCast<Date>()
GET on /helloWorld
external fun require(module: String): dynamic
external val exports: dynamic
fun main(args: Array<String>) {
val fireFunctions = require("firebase-functions")
val config = fireFunctions.config()
exports.helloWorld = fireFunctions.https.onRequest { request, response ->
console.log("Request headers: " + toJson(request.headers))
console.log("Request body: " + toJson(request.body))
response.send("Hello from Firebase!")
}
}
Deploy
Before deploy update Node.js dependencies, from command line use:
● npm update
● npm install
Build index.js file from gradle and deploy on Firebase!
● ./gradlew build
● firebase deploy
Demo: https://goo.gl/tkdvfk
Full source: https://github.com/jacklt/firebase-cloud-functions-with-kotlin-js
Real-World example: Dialogflow Fulfillment (webhook)
Telegram bot: https://t.me/gdgmilano_bot
Full source: https://github.com/jacklt/Assistant-for-events/
Google Cloud Functions: try { Kotlin } instead of JavaScript

More Related Content

What's hot

Qt Internationalization
Qt InternationalizationQt Internationalization
Qt InternationalizationICS
 
Socket.ioとBabylonJSで作ったIoT的ななにか
Socket.ioとBabylonJSで作ったIoT的ななにかSocket.ioとBabylonJSで作ったIoT的ななにか
Socket.ioとBabylonJSで作ったIoT的ななにかkamiyam .
 
Android Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyondAndroid Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyondRamon Ribeiro Rabello
 
Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Pasi Kellokoski
 
GCPUG.TW - GCP學習資源分享
GCPUG.TW - GCP學習資源分享GCPUG.TW - GCP學習資源分享
GCPUG.TW - GCP學習資源分享Simon Su
 
Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4ICS
 
Shiny r, live shared and explored
Shiny   r, live shared and exploredShiny   r, live shared and explored
Shiny r, live shared and exploredAlex Brown
 
Richard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL ModuleRichard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL ModuleAxway Appcelerator
 
Declarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeDeclarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeRamon Ribeiro Rabello
 
Qt for beginners part 1 overview and key concepts
Qt for beginners part 1   overview and key conceptsQt for beginners part 1   overview and key concepts
Qt for beginners part 1 overview and key conceptsICS
 
So I Downloaded Qt, Now What?
So I Downloaded Qt, Now What?So I Downloaded Qt, Now What?
So I Downloaded Qt, Now What?Janel Heilbrunn
 
PyCon Israel - Launch Jupyter to the Cloud
PyCon Israel - Launch Jupyter to the CloudPyCon Israel - Launch Jupyter to the Cloud
PyCon Israel - Launch Jupyter to the CloudCheuk Ting Ho
 
Introduction to using google colab
Introduction to using google colabIntroduction to using google colab
Introduction to using google colabali alemi
 
How to make GAE adapt the Great Firewall
How to make GAE adapt the Great FirewallHow to make GAE adapt the Great Firewall
How to make GAE adapt the Great FirewallHayato Yoshikawa
 
Cosla: JIRA SLA Metrics Tool in Clojure
Cosla: JIRA SLA Metrics Tool in ClojureCosla: JIRA SLA Metrics Tool in Clojure
Cosla: JIRA SLA Metrics Tool in ClojureNoah Zucker
 
GDG Kuwait - Modern android development
GDG Kuwait - Modern android developmentGDG Kuwait - Modern android development
GDG Kuwait - Modern android developmentGDGKuwaitGoogleDevel
 

What's hot (20)

TensorFlow on GCP
TensorFlow on GCPTensorFlow on GCP
TensorFlow on GCP
 
Qt Internationalization
Qt InternationalizationQt Internationalization
Qt Internationalization
 
Socket.ioとBabylonJSで作ったIoT的ななにか
Socket.ioとBabylonJSで作ったIoT的ななにかSocket.ioとBabylonJSで作ったIoT的ななにか
Socket.ioとBabylonJSで作ったIoT的ななにか
 
Android Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyondAndroid Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyond
 
Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7
 
Let's migrate to Swift 3.0
Let's migrate to Swift 3.0Let's migrate to Swift 3.0
Let's migrate to Swift 3.0
 
GCPUG.TW - GCP學習資源分享
GCPUG.TW - GCP學習資源分享GCPUG.TW - GCP學習資源分享
GCPUG.TW - GCP學習資源分享
 
Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4
 
Svelte
SvelteSvelte
Svelte
 
Shiny r, live shared and explored
Shiny   r, live shared and exploredShiny   r, live shared and explored
Shiny r, live shared and explored
 
Richard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL ModuleRichard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL Module
 
Declarative UIs with Jetpack Compose
Declarative UIs with Jetpack ComposeDeclarative UIs with Jetpack Compose
Declarative UIs with Jetpack Compose
 
Qt for beginners part 1 overview and key concepts
Qt for beginners part 1   overview and key conceptsQt for beginners part 1   overview and key concepts
Qt for beginners part 1 overview and key concepts
 
So I Downloaded Qt, Now What?
So I Downloaded Qt, Now What?So I Downloaded Qt, Now What?
So I Downloaded Qt, Now What?
 
PyCon Israel - Launch Jupyter to the Cloud
PyCon Israel - Launch Jupyter to the CloudPyCon Israel - Launch Jupyter to the Cloud
PyCon Israel - Launch Jupyter to the Cloud
 
Introduction to using google colab
Introduction to using google colabIntroduction to using google colab
Introduction to using google colab
 
Google colab introduction
Google colab   introductionGoogle colab   introduction
Google colab introduction
 
How to make GAE adapt the Great Firewall
How to make GAE adapt the Great FirewallHow to make GAE adapt the Great Firewall
How to make GAE adapt the Great Firewall
 
Cosla: JIRA SLA Metrics Tool in Clojure
Cosla: JIRA SLA Metrics Tool in ClojureCosla: JIRA SLA Metrics Tool in Clojure
Cosla: JIRA SLA Metrics Tool in Clojure
 
GDG Kuwait - Modern android development
GDG Kuwait - Modern android developmentGDG Kuwait - Modern android development
GDG Kuwait - Modern android development
 

Similar to Google Cloud Functions: try { Kotlin } instead of JavaScript

Introduction to Cloud Computing with Google Cloud
Introduction to Cloud Computing with Google CloudIntroduction to Cloud Computing with Google Cloud
Introduction to Cloud Computing with Google Cloudwesley chun
 
Exploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptExploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptwesley chun
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code維佋 唐
 
Serverless Computing with Google Cloud
Serverless Computing with Google CloudServerless Computing with Google Cloud
Serverless Computing with Google Cloudwesley chun
 
Advanced Dagger talk from 360andev
Advanced Dagger talk from 360andevAdvanced Dagger talk from 360andev
Advanced Dagger talk from 360andevMike Nakhimovich
 
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...GITS Indonesia
 
WebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webWebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webpjcozzi
 
Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Chris Ramsdale
 
JCConf 2016 - Google Dataflow 小試
JCConf 2016 - Google Dataflow 小試JCConf 2016 - Google Dataflow 小試
JCConf 2016 - Google Dataflow 小試Simon Su
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentJayaprakash R
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
Designing flexible apps deployable to App Engine, Cloud Functions, or Cloud Run
Designing flexible apps deployable to App Engine, Cloud Functions, or Cloud RunDesigning flexible apps deployable to App Engine, Cloud Functions, or Cloud Run
Designing flexible apps deployable to App Engine, Cloud Functions, or Cloud Runwesley chun
 
ES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorDavid Rodenas
 
Exploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptExploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptwesley chun
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with AndroidKurt Renzo Acosta
 
Lecture 12 - Maps, AR_VR_aaaaHardware.pptx
Lecture 12 - Maps, AR_VR_aaaaHardware.pptxLecture 12 - Maps, AR_VR_aaaaHardware.pptx
Lecture 12 - Maps, AR_VR_aaaaHardware.pptxNgLQun
 

Similar to Google Cloud Functions: try { Kotlin } instead of JavaScript (20)

Introduction to Cloud Computing with Google Cloud
Introduction to Cloud Computing with Google CloudIntroduction to Cloud Computing with Google Cloud
Introduction to Cloud Computing with Google Cloud
 
Exploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptExploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScript
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code
 
Serverless Computing with Google Cloud
Serverless Computing with Google CloudServerless Computing with Google Cloud
Serverless Computing with Google Cloud
 
Advanced Dagger talk from 360andev
Advanced Dagger talk from 360andevAdvanced Dagger talk from 360andev
Advanced Dagger talk from 360andev
 
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
 
WebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webWebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open web
 
Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010
 
JavaScript
JavaScriptJavaScript
JavaScript
 
從零開始學 Android
從零開始學 Android從零開始學 Android
從零開始學 Android
 
JCConf 2016 - Google Dataflow 小試
JCConf 2016 - Google Dataflow 小試JCConf 2016 - Google Dataflow 小試
JCConf 2016 - Google Dataflow 小試
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Designing flexible apps deployable to App Engine, Cloud Functions, or Cloud Run
Designing flexible apps deployable to App Engine, Cloud Functions, or Cloud RunDesigning flexible apps deployable to App Engine, Cloud Functions, or Cloud Run
Designing flexible apps deployable to App Engine, Cloud Functions, or Cloud Run
 
JavaScript Core
JavaScript CoreJavaScript Core
JavaScript Core
 
ES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD Calculator
 
Exploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptExploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScript
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with Android
 
Lecture 12 - Maps, AR_VR_aaaaHardware.pptx
Lecture 12 - Maps, AR_VR_aaaaHardware.pptxLecture 12 - Maps, AR_VR_aaaaHardware.pptx
Lecture 12 - Maps, AR_VR_aaaaHardware.pptx
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 

More from Omar Miatello

Kotlin: lo Swift di Android (2015)
Kotlin: lo Swift di Android (2015)Kotlin: lo Swift di Android (2015)
Kotlin: lo Swift di Android (2015)Omar Miatello
 
AARRR, Pirate Metrics with Firebase for Android (now in real time!) (2016)
AARRR, Pirate Metrics with Firebase for Android (now in real time!) (2016)AARRR, Pirate Metrics with Firebase for Android (now in real time!) (2016)
AARRR, Pirate Metrics with Firebase for Android (now in real time!) (2016)Omar Miatello
 
Firebase Remote Config + Kotlin = EasyFRC
Firebase Remote Config + Kotlin = EasyFRCFirebase Remote Config + Kotlin = EasyFRC
Firebase Remote Config + Kotlin = EasyFRCOmar Miatello
 
Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03Omar Miatello
 
Android & Kotlin - The code awakens #02
Android & Kotlin - The code awakens #02Android & Kotlin - The code awakens #02
Android & Kotlin - The code awakens #02Omar Miatello
 
Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Omar Miatello
 
Kotlin - lo Swift di Android
Kotlin - lo Swift di AndroidKotlin - lo Swift di Android
Kotlin - lo Swift di AndroidOmar Miatello
 

More from Omar Miatello (8)

Kotlin: lo Swift di Android (2015)
Kotlin: lo Swift di Android (2015)Kotlin: lo Swift di Android (2015)
Kotlin: lo Swift di Android (2015)
 
AARRR, Pirate Metrics with Firebase for Android (now in real time!) (2016)
AARRR, Pirate Metrics with Firebase for Android (now in real time!) (2016)AARRR, Pirate Metrics with Firebase for Android (now in real time!) (2016)
AARRR, Pirate Metrics with Firebase for Android (now in real time!) (2016)
 
Firebase Remote Config + Kotlin = EasyFRC
Firebase Remote Config + Kotlin = EasyFRCFirebase Remote Config + Kotlin = EasyFRC
Firebase Remote Config + Kotlin = EasyFRC
 
Google Wave (2010)
Google Wave (2010)Google Wave (2010)
Google Wave (2010)
 
Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03
 
Android & Kotlin - The code awakens #02
Android & Kotlin - The code awakens #02Android & Kotlin - The code awakens #02
Android & Kotlin - The code awakens #02
 
Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01
 
Kotlin - lo Swift di Android
Kotlin - lo Swift di AndroidKotlin - lo Swift di Android
Kotlin - lo Swift di Android
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 

Google Cloud Functions: try { Kotlin } instead of JavaScript

  • 1. Google Cloud Functions: try { Kotlin } insteadOf JavaScript How to write (almost) free backend services on Google Cloud Functions in Kotlin
  • 2. Omar Miatello Android Developer @ Organizer @ Personal profile google.com/+OmarMiatello Slides available on: Google Presentation https://goo.gl/CMVNqS Google Photo https://goo.gl/HpRLw4 Speaker Deck (by GitHub) https://speakerdeck.com/jacklt SlideShare (by LinkedIn) https://www.slideshare.net/OmarMiatello
  • 3. I’m an Android developer (since 2010) About me
  • 4. I’m an Android developer (since 2010) I use Kotlin (since 2015) instead of Java About me
  • 5. I’m an Android developer (since 2010) I use Kotlin (since 2015) instead of Java I use a lot of Firebase services About me
  • 6. Target: cost-free backend! Free hosting options: ● PHP (a lot of free hosting, since 2000) ○ Altervista, Netsons, … ● Solution PaaS (Platform as a Service) ○ Google App Engine ■ Support for: Node.js, Java, Ruby, C#, Go, Python e PHP, … ■ 28 hours instance a day ○ Amazon Web Services (Free tear only for the first 12 months) ● Solution “serverless”
  • 7. Target: cost-free backend! Comparison free "serverless" plans: ● Google Cloud Functions ○ Node.js ○ 2.000.000 free calls/month ● Cloud Functions for Firebase ○ Node.js ○ 125.000 / 2.000.000 free calls/month ● AWS Lambda ○ Node.js, Java, C#, Go, Python ○ 1.000.000 free calls/month
  • 8. 2.000.000 calls/month? What if: I call a function every second 1 day = 86.400 seconds ~ 12 days = 1.000.000 seconds ~ 23 days = 2.000.000 seconds 30 days = 2.592.000 seconds
  • 9. 2.000.000 calls/month? How many users? 1 day = ~ 65.000 calls depends on Average User Activity 100 calls/day = ~ 650 users/day 10 calls/day = ~ 6.500 users/day
  • 10. Setup: Cloud Functions + Kotlin (Node.js) + Kotlin for JavaScript picture: https://blog.jetbrains.com/kotlin/2017/11/kotlin-1-2-released
  • 11. Setup: Cloud Functions + Kotlin (Node.js) + Kotlin for JavaScript picture: https://blog.jetbrains.com/kotlin/2017/11/kotlin-1-2-released
  • 12. New Project in Kotlin/JS ● Download: IntelliJ IDEA Community Edition (Free) ○ https://www.jetbrains.com/idea/download ● New Project: Gradle > Kotlin (JavaScript)
  • 13. New Project in Kotlin/JS ● Download: IntelliJ IDEA Community Edition (Free) ○ https://www.jetbrains.com/idea/download ● New Project: Gradle > Kotlin (JavaScript) ● GroupId: “com.example” & ArtifactId: “myservice” ● Confirm default settings until “Finish”
  • 14. Configure: Cloud Functions for Firebase ● Create new project on Firebase (optional) ○ Use console: https://console.firebase.google.com
  • 15. Configure: Cloud Functions for Firebase ● Create new project on Firebase (optional) ○ Use console: https://console.firebase.google.com ● Open Functions section > click “Start”
  • 16. Configure: Cloud Functions for Firebase ● Create new project on Firebase (optional) ○ Use console: https://console.firebase.google.com ● Open Functions section > click “Start” ● Install Node.js: https://nodejs.org ○ Command line: “npm install -g firebase-tools” and “npm install kotlin” ○ Open Terminal in project folder and run “firebase init” (and follow instructions)
  • 17. Configure: Gradle ● Add these lines in build.gradle ● Ready! ○ Deploy command: firebase deploy compileKotlin2Js.kotlinOptions { moduleKind = "commonjs" outputFile = "functions/index.js" }
  • 18. Kotlin for JavaScript JavaScript is a dynamically-typed language, which means it does not check types at compile-time. You can freely talk to JavaScript from Kotlin via dynamic types, but if you want the full power of Kotlin type system, you can create Kotlin headers for JavaScript libraries.
  • 19. Kotlin: useful keywords for JavaScript Keywords: ● external - Modifier to tell Kotlin that a certain declaration is written in pure JavaScript. Compiler assumes that the implementation for the corresponding class, function or property is provided by the developer. ● dynamic - The dynamic type basically turns off Kotlin's type checker. The most peculiar feature of dynamic is that we are allowed to call any property or function with any parameters on a dynamic variable.
  • 20. Kotlin: useful functions for JavaScript Extensions: ● .asDynamic() - Reinterprets this value as a value of the dynamic type. ● .unsafeCast<T>() - Reinterprets this “dynamic” value as a value of the specified type [T] (ex: String / Array) without any actual type checking. Methods: ● js(code) - Puts the given piece of a JavaScript code right into the calling function. The compiler replaces call to `js(...)` code with the string constant provided as a parameter.
  • 21. Example: Utils.kt fun toJson(obj: dynamic) = JSON.stringify(obj) { key, value -> value ?: undefined } // Arrays / Map utils fun jsMap(init: (dynamic) -> Unit) = Any().apply { init(asDynamic()) }.asDynamic() fun keys(obj: dynamic) = js("Object").keys(obj).unsafeCast<Array<String>>() // JavaScript functions fun getDateOfWeek(week: Int, year: Int) = js(""" var simple = new Date(year, 0, 1 + (week - 1) * 7); var dow = simple.getDay(); var iso = simple; if (dow <= 4) iso.setDate(simple.getDate() - simple.getDay() + 1); else iso.setDate(simple.getDate() + 8 - simple.getDay()); return iso;""").unsafeCast<Date>()
  • 22. GET on /helloWorld external fun require(module: String): dynamic external val exports: dynamic fun main(args: Array<String>) { val fireFunctions = require("firebase-functions") val config = fireFunctions.config() exports.helloWorld = fireFunctions.https.onRequest { request, response -> console.log("Request headers: " + toJson(request.headers)) console.log("Request body: " + toJson(request.body)) response.send("Hello from Firebase!") } }
  • 23. Deploy Before deploy update Node.js dependencies, from command line use: ● npm update ● npm install Build index.js file from gradle and deploy on Firebase! ● ./gradlew build ● firebase deploy Demo: https://goo.gl/tkdvfk Full source: https://github.com/jacklt/firebase-cloud-functions-with-kotlin-js
  • 24. Real-World example: Dialogflow Fulfillment (webhook) Telegram bot: https://t.me/gdgmilano_bot Full source: https://github.com/jacklt/Assistant-for-events/