concept
Progressive Web Apps are Progressive Web applications that run in modern browsers and exhibit superpowers. Support discoverability, installability, offline operation, message push and other APP specific capabilities. This project encapsulates the realization of message push function in NodeJS in the simplest way.
KOA2-PWA
Koa2-pwa is a simple implementation of the PWA push server, developed using KOA2, stored using mysql database, and managed processes and logs using PM2.
View PWA front-end project implementation
Operation process
The PWA message push function relies on the Service Worker (SW) using the VAPID protocol.
-> Create vapidKeys(publicKey,privateKey) using web-push on server -> Save publicKey,privateKey, Front-end saves the publicKey -> Front-end SW uses the encrypted publicKey to subscribe and obtain the subscription object, and then saves the subscription object to the server -> The server obtains the subscription object when it needs to push
In the project directory, app.js is the startup file, and /bin/ WWW is the package of the startup file. Generally, WWW is used to start the service. /public is a web static file, /views is a web template; /db /routes /routes /db /routes
// /db/config.js saves database login information
const config = {
host: * * *,user: * * *,password: * * *,database: '* * *'.// Database name, custom
port: ***
}
module.exports = config
// /db/index.js Database related operations
const mysql = require('mysql')
const config = require('./config')
const pool = mysql.createPool(config)
// some sql
module.exports = {
// some methods
}
Copy the code
/routes/notification.js: provides more notification options for pushing messages
const router = require('koa-router') ()const webpush = require('web-push')
const dbModel = require('.. /db/index')
// VAPID keys should only be generated only once.
// const vapidKeys = webpush.generateVAPIDKeys()
const publicKey = '* * *'
const privateKey = '* * *'
const gcmapiKey = 'PWA_LITE_GCMAPIKey' // Custom, stored in front manifest.json
const mailto = 'mailto:[email protected]'
// send notification to client
const sendNotification = (pushSubscription, body) = > {
return webpush.sendNotification(pushSubscription, JSON.stringify(body))
}
webpush.setGCMAPIKey(gcmapiKey)
webpush.setVapidDetails(mailto, publicKey, privateKey)
// router prefix
router.prefix('/api/notification')
// user subscribe
router.post('/subscribe'.async (ctx, next) => {
let body = ctx.request.body
let user = [body.authSecret, body.key, body.endpoint]
let pushSubscription = {
endpoint: body.endpoint,
keys: {
auth: body.authSecret,
p256dh: body.key
}
}
let body = {
title: 'Congratulations'.message: `Hello,Thank you for subscribtion! `.data: {}
}
sendNotification(pushSubscription, body)
// do something
})
module.exports = router
Copy the code
start
github
Koa2 requires NodeJS 7.6 or later.
# install pm2
npm install -g pm2
# install dependencies
npm install
# serve with hot reload at localhost:3000
npm start
# run with pm2
npm run prd
# or pm2 start bin/www
Copy the code