SlideShare a Scribd company logo
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

PPTX
Introduction to Serverless and Google Cloud Functions
PDF
The Next Generation Qt Item Views
PDF
Qt Graphics View Framework (Qt Developers Meetup Isreal)
PDF
AllTheTalks 2020: Buildpacks - container for everyone!
PPTX
Monads Transformation
PDF
Architecture for scalable Angular applications
PDF
Rpy2 demonstration
PDF
Architecture for scalable Angular applications (with introduction and extende...
Introduction to Serverless and Google Cloud Functions
The Next Generation Qt Item Views
Qt Graphics View Framework (Qt Developers Meetup Isreal)
AllTheTalks 2020: Buildpacks - container for everyone!
Monads Transformation
Architecture for scalable Angular applications
Rpy2 demonstration
Architecture for scalable Angular applications (with introduction and extende...

What's hot (20)

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

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

PPTX
Introduction to Kotlin Language and its application to Android platform
PPTX
K is for Kotlin
PDF
Kotlin 1.2: Sharing code between platforms
PDF
Kotlin, smarter development for the jvm
PDF
Develop your next app with kotlin @ AndroidMakersFr 2017
PDF
Davide Cerbo - Kotlin loves React - Codemotion Milan 2018
PDF
Introduction to kotlin
PDF
Kotlin: A pragmatic language by JetBrains
PPTX
KotlinForJavaDevelopers-UJUG.pptx
PDF
Intro to Kotlin
PDF
Kotlin what_you_need_to_know-converted event 4 with nigerians
PDF
Kotlin @ Devoxx 2011
PDF
Kotlin Slides from Devoxx 2011
PDF
Little Helpers for Android Development with Kotlin
PDF
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
PDF
Kotlin & Arrow the functional way
PDF
Swift and Kotlin Presentation
PDF
Kotlin for Android - Vali Iorgu - mRready
PDF
Rapid Web API development with Kotlin and Ktor
Introduction to Kotlin Language and its application to Android platform
K is for Kotlin
Kotlin 1.2: Sharing code between platforms
Kotlin, smarter development for the jvm
Develop your next app with kotlin @ AndroidMakersFr 2017
Davide Cerbo - Kotlin loves React - Codemotion Milan 2018
Introduction to kotlin
Kotlin: A pragmatic language by JetBrains
KotlinForJavaDevelopers-UJUG.pptx
Intro to Kotlin
Kotlin what_you_need_to_know-converted event 4 with nigerians
Kotlin @ Devoxx 2011
Kotlin Slides from Devoxx 2011
Little Helpers for Android Development with Kotlin
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
Kotlin & Arrow the functional way
Swift and Kotlin Presentation
Kotlin for Android - Vali Iorgu - mRready
Rapid Web API development with Kotlin and Ktor
Ad

More from Omar Miatello (8)

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

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Modernizing your data center with Dell and AMD
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
Teaching material agriculture food technology
PDF
Encapsulation theory and applications.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
KodekX | Application Modernization Development
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Dropbox Q2 2025 Financial Results & Investor Presentation
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Big Data Technologies - Introduction.pptx
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Monthly Chronicles - July 2025
Modernizing your data center with Dell and AMD
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Teaching material agriculture food technology
Encapsulation theory and applications.pdf
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf
Encapsulation_ Review paper, used for researhc scholars
KodekX | Application Modernization Development
Diabetes mellitus diagnosis method based random forest with bat algorithm

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/