1
0
Fork 0
mirror of https://github.com/imjasonh/webpush synced 2026-07-06 23:52:25 +00:00
webpush/example
Jason Hall ee97c9b955 deploy script, use kms
Signed-off-by: Jason Hall <jason@chainguard.dev>
2025-11-25 11:04:19 -06:00
..
static log more, require interaction 2025-11-24 18:47:50 -06:00
.gitignore Add example directory with demo server for web push notifications 2025-11-24 21:39:48 +00:00
main.go deploy script, use kms 2025-11-25 11:04:19 -06:00
README.md Add example directory with demo server for web push notifications 2025-11-24 21:39:48 +00:00

Web Push Example

A complete example demonstrating the web push library with:

  • VAPID keys stored on disk (auto-generated on first run)
  • SQLite storage for subscriptions
  • Web client for subscribing to notifications
  • Automatic push every minute
  • Manual push via /ping endpoint

Running

cd example
go run main.go

Then open http://localhost:8080 in your browser.

Features

  1. Subscribe: Click "Subscribe to Notifications" to register for push notifications
  2. Automatic Pushes: Notifications are sent every minute to all subscribers
  3. Manual Pushes: Visit /ping or click "Send Ping" to trigger an immediate push

Files Generated

On first run, the example generates:

  • vapid-private.pem - VAPID private key (keep this secret!)
  • subscriptions.db - SQLite database for storing subscriptions

Endpoints

  • GET / - Web client UI
  • GET /sw.js - Service worker for handling push events
  • GET /api/vapid-public-key - Returns the VAPID public key
  • POST /api/subscribe - Register a new subscription
  • POST /api/unsubscribe - Remove a subscription
  • GET /ping - Send push to all subscribers (optional ?title= and ?body= query params)