Skip to content

Instantly share code, notes, and snippets.

@CodingDoug
Last active December 14, 2022 09:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save CodingDoug/490f9222c8b0f696338e2d74fcb78594 to your computer and use it in GitHub Desktop.
Save CodingDoug/490f9222c8b0f696338e2d74fcb78594 to your computer and use it in GitHub Desktop.
Realtime Database triggers with Cloud Functions for Firebase - source

Realtime Database triggers with Cloud Functions for Firebase - source

This gist contains the source code in my video series about Realtime Database triggers. You can watch the three parts here:

  1. Part 1 (intro, onCreate)
  2. Part 2 (onUpdate, infinite loops)
  3. Part 3 (onDelete, transactions)

index.ts contains the Cloud Functions code, and dialog.ts contains the script to run locally that simulates the addition of messages to the chat room.

The code in this project is licensed under the Apache License 2.0.

Copyright 2018 Google LLC
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    https://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as admin from 'firebase-admin'
const serviceAccount = require('../service-account.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: `https://${serviceAccount.project_id}.firebaseio.com`
})
;(async () => {
await chat('pizzachat', 'Fear', "What the heck is that?!")
await chat('pizzachat', 'Joy', "Who puts broccoli on pizza?!")
await chat('pizzachat', 'Disgust', "That's it. I'm done.")
await chat('pizzachat', 'Anger', "Congratulations, San Francisco! You've ruined pizza!")
process.exit(0)
})()
.catch(err => { console.error(err) })
async function chat(room: string, name: string, text: string) {
const messagesRef = admin.database().ref('rooms').child(room).child('messages')
await messagesRef.push({ name, text })
console.log(`${name}: ${text}`)
await sleep(1500)
}
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as functions from 'firebase-functions'
export const onMessageCreate = functions.database
.ref('/rooms/{roomId}/messages/{messageId}')
.onCreate(async (snapshot, context) => {
const roomId = context.params.roomId
const messageId = context.params.messageId
console.log(`New message ${messageId} in room ${roomId}`)
const messageData = snapshot.val()
const text = addPizzazz(messageData.text)
await snapshot.ref.update({ text: text })
const countRef = snapshot.ref.parent.parent.child('messageCount')
return countRef.transaction(count => {
return count + 1
})
})
export const onMessageDelete = functions.database
.ref('/rooms/{roomId}/messages/{messageId}')
.onDelete(async (snapshot, context) => {
const countRef = snapshot.ref.parent.parent.child('messageCount')
return countRef.transaction(count => {
return count - 1
})
})
function addPizzazz(text: string): string {
return text.replace(/\bpizza\b/g, '🍕')
}
export const onMessageUpdate = functions.database
.ref('/rooms/{roomId}/messages/{messageId}')
.onUpdate((change, context) => {
const before = change.before.val()
const after = change.after.val()
if (before.text === after.text) {
console.log("Text didn't change")
return null
}
const text = addPizzazz(after.text)
const timeEdited = Date.now()
return change.after.ref.update({ text, timeEdited })
})
@Shaunmax
Copy link

Hello,

I was following your instructions in the youtube video. But when using change.after.val() - I am getting error: - Object is possibly 'undefined'.ts(2532) (parameter) change: functions.Change<functions.database.DataSnapshot>

Could you please look into it and let me know what was the issue.

Thanks
Shaun

@samizuh
Copy link

samizuh commented Mar 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment