cotrone / web-push

Web Push library in Haskell.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

web-push

Hackage Spec and Mock Tests Example Browser Tests

Send web push notifications to browsers

Forked from https://github.com/sarthakbagaria/web-push

Usage

Guides to using Web Push API in browsers can be found on Mozilla's and Google's docs, or you can check out the demo app in the example folder. To run the demo app:

with nix: nix run .#web-push-example:exe:web-push-example-server

with cabal: cabal run web-push-example-server

stack: stack exec web-push-example-server

Then access localhost:3000 from a browser

For production use, store a set of VAPID keys securely and use them for all push notification subscriptions and messages; public key will have to be exposed to client's browser when subscribing to push notifications, but private key must be kept secret and used when generating push notifications on the server. If VAPID keys are re-generated, all push notifications will require re-subscriptions. Also save the latest subscription details such as endpoint from user's browser session securely in the database and use them to send push notifications to the user later.

Generating Keys

Generating VAPID keys should be done with openssl.

To generate the private key:

openssl ecparam -name prime256v1 -genkey -noout -out vapid-private.pem

to calculate the public key from the private key:

openssl ec -in vapid-private.pem -pubout -out vapid-public.pem

Build

The library builds and tests run with both cabal and stack.

There is also a nix flake setup to manage the dependencies for tests.

To build a cabal environment for the project that

Nix

The nix flake includes:

  • A development environment for web-push and web-push-example through nix develop
  • A package for the example server nix run .#web-push-example-server
  • A package for the mock web push server from https://github.com/marc1706/web-push-testing
    • nix run .#web-push-testing -- start and nix run .#web-push-testing -- stop to control it
    • Launch the server directly with nix run .#web-push-testing-server -- 8090
  • Tests for web-push and web-push-example
    • For web-push: nix run .#web-push-test
    • For web-push-example: nix run .#web-push-example-test
  • Browser tests for the web-push-example server with nix run .#web-push-example-test

Example

There is a minimal example web application that generates keys, stores subscriptions, and sends notifications to all subscribers in web-push-example

References

Current implementation is based on the following versions of the drafts:

About

Web Push library in Haskell.

License:MIT License


Languages

Language:Haskell 78.2%Language:Mustache 12.3%Language:Nix 7.2%Language:JavaScript 1.4%Language:HTML 0.9%