npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpush-client

v0.4.2

Published

My New Project description.

Downloads

733

Readme

WebPush Client

Handles subscription / unsubscription of Webpush notifications in sync with a remote server.

Installation

The library ships with both a client and a service worker.

In a browser

  • Copy dist/webpush-client.min.js and dist/webpush-sw.js in your project.

In a JS project

  • Run yarn add webpush-client or npm install --save webpush-client

Configuration

In a browser

<script src="/js/webpush-client.js"></script>
<script>

    var WebPushClient;
    if (WebPushClientFactory.isSupported()) {
        WebPushClientFactory.create({
            serviceWorkerPath: '/js/webpush-sw.js', // Public path to the service worker
            serverKey: 'my_server_key', // https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol#application_server_keys
            subscribeUrl: '/webpush', // Optionnal - your application URL to store webpush subscriptions
        })
            .then(Client => {
                WebPushClient = Client;
            })
        ;
    }
</script>

In a JS project

import Webpush from 'webpush-client'

Webpush.create({
    serviceWorkerPath: '/js/webpush-sw.js', // Public path to the service worker
    serverKey: 'my_server_key', // https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol#application_server_keys
    subscribeUrl: '/webpush', // Optionnal - your application URL to store webpush subscriptions
})
    .then(WebPushClient => {
        // do stuff with WebPushClient
    })
;

Usage

  • WebPushClient.isSupported()

Return whether or not the browser AND the server both support Push notifications.

  • WebPushClient.getPermissionState()

Return if Push notifications are allowed for your domain. Possible values: prompt, granted, denied

  • WebPushClient.getSubscription()

Will return null if not subscribed or a PushSubscription object.

  • WebPushClient.subscribe([options])

Subscribe browser to Push Notifications. The browser will ask for permission if needed. Return a Promise which resolves to the PushSubscription object (or will be rejected in case of access denied)

  • WebPushClient.unsubscribe([options])

Subscribe browser to Push Notifications. The browser will ask for permission if needed. Return a Promise which resolves to the PushSubscription object (or will be rejected in case of access denied)

subscribeUrl

When the subscribeUrl option is provided, WebPushClient.subscribe([options]) will POST a JSON containing both the PushSubscription and the options objects:

{
  "subscription": {
    "endpoint": "http://endpoint",
    "keys": {
      "p256dh": "public key",
      "auth": "private key"
    }
  },
  "options": {}
}

The request will include current credentials allowing you to associate the current user logged in to the PushSubscription object and an optionnal, arbitrary options object of your own.

WebPushClient.unsubscribe([options]) will issue a DELETE request on subscribeUrl with the same body.

It's now up to you to handle these infos properly on the server side. Have a look at the RemoteStorage class to check out how these requests are generated.

If your application runs on Symfony, you can have a look at bentools/webpush-bundle for which this JS was originally designed for.