Demo backend + frontend for implementing push notifications using service workers
Most of the frontend is a modified version of the code from web-push codelabs. But it also adds support for sending notification, unsubscription and saving/deleting the subscription from db
Built with Typescript + Express + Pg-Promise (Postgresql) + EJS
The backend uses web-push to handle the web push setup and notifications
-
Clone the repo using
git clone https://github.com/TotallyNotChase/web-push-tryout.git
-
cd
intoweb-push-tryout
-
Execute
npm i
to set up the packages -
Execute
./node_modules/.bin/web-push generate-vapid-keys
and save the output for later use -
Set up the user configurations, create a file named
userconfig.json
insideweb-push-tryout
The format of the JSON should be-
{ "NAME": <app_name>, "PORT": <port_to_assign_the_server_on>, "ENV": "development"/"production", "DB_CONNECTION": { "host": <postgres_host>, "port": <postgres_port>, "database": <database_name>, "user": <postgres_username>, "password": <postgres_userpass> }, "PUBLIC_VAPID_KEY": <Public_VAPID_key_generated_from_web-push>, "PRIVATE_VAPID_KEY": <Private_VAPID_key_generated_from_web-push>,, "WEB_PUSH_CONTACT": <mailto_link_to_an_email_to_be_associated_with_web_push> }
Fieldname Description NAME
The name of the webapp - can be whatever
PORT
The port to assign the webserver on (should be a number - not string)
ENV
The environment to run the express server on - usually
development
DB_CONNECTION
Postgre DB connection object
host
is usually"localhost"
andport
is5432
(should be a number - not string) for a default configured postgre server running locallydatabase
should be a newly created database on the serveruser
andpassword
are the credentials for the postgre userPUBLIC_VAPID_KEY
Public VAPID key generated from
web-push generate-vapid-keys
on one of the previous stepsPRIVATE_VAPID_KEY
Private VAPID key generated from
web-push generate-vapid-keys
on one of the previous stepsWEB_PUSH_CONTACT
A
mailto
link to the mail contact associated with the server - should look likemailto: <youremail>
-
Execute
npm run build
to build the projectThis will transpile or the typescript files into javascipt and put them into
dist/
It also moves any other necessary non-typescript files from
src/
todist/
(handled bycopyRes.js
) -
Execute
npm run serve
to run the server
All the backend code related stuff is in src/
, static assets for the frontend are in assets/
Component | Description |
---|---|
|
Controllers for different routes |
|
Component that manages low level SQL queries using pg-promise and exposes a high level, ORM-like API to be used by the app
|
|
Handles middleware configuration and exposes middleware objects to be used by the app |
|
Routers that map controllers to corresponding routes - exposes a router object to be used by the app |
|
Extra types used across the project |
|
Component that has the web-push API exposed to itself Manages the web-push calls |
The only files that need to be discussed about are the index.ts
and sw.ts
files that handle all the push notifications on the frontend side
Makes sure the browser supports push notifications
Initiallized the UI, based on initial state of subscription
Sets up handlers for the subscribe and push buttons
This is more or less a modified version of the codelabs main.js
, except it adds support for communicating with the backend through updateSubscriptionOnServer
, deleteSubscriptionOnServer
, initiatePushOnServer
. It also adds support for unsubscription on both the frontend and backend, as well as sending the notification itself through the backend.
Service worker file that sets up event listener for the push notification to be sent