graphql-engine/community/sample-apps/serverless-push
surendran82 6acb7152cb community sample apps: image broken link update for SEO
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7170
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
GitOrigin-RevId: 145831d1056922af70150f8193c7f868de756f8f
2022-12-06 11:04:21 +00:00
..
cloudfunction refactor community content and folder structure (#1353) 2019-01-17 15:57:28 +05:30
hasura community: fix sample apps metadata for v3 config 2021-08-19 03:46:59 +00:00
arch.png add github workflow to compress new images in PRs 2021-03-10 20:55:02 +00:00
Dockerfile refactor community content and folder structure (#1353) 2019-01-17 15:57:28 +05:30
favicon.png community sample apps: image broken link update for SEO 2022-12-06 11:04:21 +00:00
firebase-messaging-sw.js refactor community content and folder structure (#1353) 2019-01-17 15:57:28 +05:30
hasura_logo.png add github workflow to compress new images in PRs 2021-03-10 20:55:02 +00:00
index.css refactor community content and folder structure (#1353) 2019-01-17 15:57:28 +05:30
index.html community: update paths for sample-apps (close #1915) (#2723) 2019-08-14 07:05:44 +00:00
index.js community: update sample apps to use hasura cloud and v3 migrations/metadata 2021-03-02 08:32:37 +00:00
manifest.json refactor community content and folder structure (#1353) 2019-01-17 15:57:28 +05:30
package.json community: update sample apps for codesandbox compatibility (#2377) 2019-06-14 14:37:00 +05:30
README.md readme: use cloud signup endpoint for "Deploy to Hasura" CTA 2021-08-19 07:03:05 +00:00

Web notifications using Hasura GraphQL Engine Event Triggers and FCM

Visit https://serverless-push.demo.hasura.app/ for a live demo.

Edit serverless-push

Instructions

Pre-requisites

  • Google Cloud account with billing enabled

  • Firebase Cloud Messaging (FCM) account

  • Hasura GraphQL Engine (HGE) installation

  • Deploy GraphQL Engine on Hasura Cloud and setup PostgreSQL via Heroku:

    Deploy to Hasura Cloud

Step 1: Create table

Goto HGE console and create the following table:

Table name: message

Columns:

id            UUID        default: gen_random_uuid()
timestamp     Timestamp   default: now()
title         Text
body          Text        nullable
device_token  Text

Primary key: id

Step 2: Setup FCM

  • Create a new project or use an existing project on Firebase Console.
  • Note down the Google Cloud project name - also called PROJECT_ID.
  • Open the Cloud Messaging tab of the Firebase console Settings pane and scroll to the Web configuration section.
  • In the Web Push certificates tab, click Generate Key Pair. The console displays a notice that the key pair was generated, and displays the public key string and date added.
  • Copy the Key Pair (we'll call it FCM_PUBLIC_KEY).
  • Note down the Sender ID from top of this page (SENDER_ID);
  • Also note down the Server Key (SERVER_KEY).

Step 3: Deploy Google Cloud Function

We'll configure HGE to send an event to a webhook trigger whenever an insert happens on the message table. The data inserted to this table contains the notification text and a unique device token. The webhook receives this data and makes an API call to Firebase to send a push notification to that unique device.

We'll deploy this webhook as a Google Cloud Function.

  • Execute the command to deploy the function:
    cd cloudfunction
    gcloud beta functions deploy push-notification \
           --runtime nodejs8 --trigger-http \
           --set-env-vars="FCM_SERVER_KEY=<SERVER_KEY>"
    
  • Copy URL from the output (TRIGGER_URL)
    httpsTrigger:
      url: https://us-central1-hasura-test.cloudfunctions.net/push-notification
    

Step 4: Setup event trigger

Goto HGE console and then to the "Events" tab. Here we can add triggers which will be executed on insert/update/delete actions on tables configured.

Add a new trigger with details as follows:

Trigger name:  message_event
Schema/Table:  public/message
Operations:    Insert
Webhook URL:   <TRIGGER_URL>

Use the TRIGGER_URL from the cloudfunction deployment.

Step 5: Add configuration variables

  • Edit index.html, add PROJECT_ID and SENDER_ID.
  • Edit index.js, add FCM_PUBLIC_KEY and Hasura GraphQL Engine url (HGE_URL)

Step 6: Run the server

Run any HTTP server locally and visit the URL on browser.

http-server

Note

: If you're deploying to any server other than localhost, service workers will only work if the scheme is HTTPS.

Architecture

architecture diagram