Skip to content

Instantly share code, notes, and snippets.

@CodingDoug
Last active December 14, 2022 09:48
Show Gist options
  • Select an option

  • Save CodingDoug/490f9222c8b0f696338e2d74fcb78594 to your computer and use it in GitHub Desktop.

Select an option

Save CodingDoug/490f9222c8b0f696338e2d74fcb78594 to your computer and use it in GitHub Desktop.

Revisions

  1. CodingDoug revised this gist Aug 7, 2018. No changes.
  2. CodingDoug revised this gist Aug 1, 2018. 3 changed files with 50 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -9,3 +9,21 @@ You can watch the three parts here:

    `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.

    ```text
    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.
    ```
    16 changes: 16 additions & 0 deletions dialog.ts
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,19 @@
    /*
    * 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({
    16 changes: 16 additions & 0 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,19 @@
    /*
    * 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
  3. CodingDoug created this gist Aug 1, 2018.
    11 changes: 11 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # 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](https://www.youtube.com/watch?v=DglTSNEdl0U) (intro, onCreate)
    1. [Part 2](https://www.youtube.com/watch?v=Bdm7QNwSHOg) (onUpdate, infinite loops)
    1. [Part 3](https://www.youtube.com/watch?v=TPKA88_FmkA) (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.
    26 changes: 26 additions & 0 deletions dialog.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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));
    }
    47 changes: 47 additions & 0 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    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 })
    })