How to use web-push - 10 common examples

To help you get started, we’ve selected a few web-push examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github googlearchive / Propel / test / data / node-backend / index.js View on Github external
const webPush = require('web-push');

const args = require('minimist')(process.argv.slice(2));

if (!args.subscription || !args.payload) {
  throw new Error('Bad Input - Sorry. Include --subscription and --payload');
}

// Just makes final output a little cleaner
console.log('');
console.log('');

const subscription = JSON.parse(args.subscription);
const payload = args.payload;

webPush.setGCMAPIKey('AIzaSyBBh4ddPa96rQQNxqiq_qQj7sq1JdsNQUQ');

return webPush.sendNotification(subscription.endpoint, {
  userPublicKey: subscription.keys.p256dh,
  userAuth: subscription.keys.auth,
  payload: payload
})
.then(result => {
  console.log('Result: ', result);
})
.catch(err => {
  console.log(err);
})
.then(() => {
  // Just makes final output a little cleaner
  console.log('');
  console.log('');
github timothyarmes / ta-meteor-apollo-starter-kit / app / api / users / server / resolvers / mutation / send-push-notification.js View on Github external
subscriptions.forEach((subscription) => {
    webPush.sendNotification(subscription, payload, options)
      .then(() => {})
      .catch((err) => {
        console.log(`Error when trying to deliver message for ${subscription.endpoint}`, err);
        // This is probably an old subscription, remove it
        deleteSubscription(null, { endpoint: subscription.endpoint }, { user });
      });
  });
};
github lunchclass / absolute / server / push / controller / send_push.js View on Github external
webpush.setVapidDetails(
      SERVER_SUBJECT,
      pushKeys.pushVapidKeys.publicKey,
      pushKeys.pushVapidKeys.privateKey,
    );

    // This is the same output of calling JSON.stringify on a PushSubscription
    const pushSubscription = {
      endpoint: endpoint,
      keys: {
        auth: keys.auth,
        p256dh: keys.p256dh,
      },
    };

    webpush.sendNotification(pushSubscription, payload)
    .then((webPushResult) => {
      return resolve(webPushResult);
    }).catch((webPushError) => {
      console.log(`push failed to ${endpoint}\n ${webPushError}`);
      return reject(webPushError);
    });
  });
}
github mozilla / serviceworker-cookbook / push-get-payload / server.js View on Github external
// Use the web-push library to hide the implementation details of the communication
// between the application server and the push service.
// For details, see https://tools.ietf.org/html/draft-ietf-webpush-protocol and
// https://tools.ietf.org/html/draft-ietf-webpush-encryption.
const webPush = require('web-push');

if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
  console.log("You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY "+
    "environment variables. You can use the following ones:");
  console.log(webPush.generateVAPIDKeys());
  return;
}
// Set the keys used for encrypting the push messages.
webPush.setVapidDetails(
  'https://serviceworke.rs/',
  process.env.VAPID_PUBLIC_KEY,
  process.env.VAPID_PRIVATE_KEY
);

const payloads = {};

module.exports = function(app, route) {
  app.get(route + 'vapidPublicKey', function(req, res) {
    res.send(process.env.VAPID_PUBLIC_KEY);
  });

  app.post(route + 'register', function(req, res) {
    // A real world application would store the subscription info.
    res.sendStatus(201);
  });
github lowercasename / sweet / server.js View on Github external
})
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
app.use(function (req, res, next) {
  res.locals.sessionFlash = req.session.sessionFlash;
  delete req.session.sessionFlash;
  next();
});

//set up webpush to send push notifications for the notifier
webpush = require('web-push');
if (!auth.vapidPrivateKey || !auth.vapidPublicKey) {
  vapidKeys = webpush.generateVAPIDKeys();
  webpush.setVapidDetails(
    'mailto:support@sweet.sh',
    vapidKeys.publicKey,
    vapidKeys.privateKey
  );
} else {
  webpush.setVapidDetails(
    'mailto:support@sweet.sh',
    auth.vapidPublicKey,
    auth.vapidPrivateKey
  );
}

//kill the process when the sigint code is recieved, generally generated by pressing ctrl-c in the console
app.on('SIGINT', function () {
  db.stop(function (err) {
    process.exit(err ? 1 : 0);
github webmaxru / pwa-guide-ngpoland / push-server.js View on Github external
var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());
app.options('*', cors());

var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
var PORT = process.env.PORT || 8090;
var webPush = require('web-push');

// The GCM API key is AIzaSyDNlm9R_w_0FDGjSM1fzyx5I5JnJBXACqU
webPush.setVapidDetails(
  'mailto:salnikov@gmail.com',
  'BHe82datFpiOOT0k3D4pieGt1GU-xx8brPjBj0b22gvmwl-HLD1vBOP1AxlDKtwYUQiS9S-SDVGYe_TdZrYJLw8',
  's-zBxZ1Kl_Y1Ac8_uBjwIjtLtG6qlJKOX5trtbanAhc'
);

app.use(express.static(__dirname));

app.use(bodyParser.json());

app.use("/push", function(req, res, next) {
  //console.log(res.body);
  if (req.body.action === 'subscribe') {
    var endpoint = req.body.subscription;

    console.log(req);
github OriginProtocol / origin / origin-notifications / src / app.js View on Github external
let privateKey = process.env.VAPID_PRIVATE_KEY
let publicKey = process.env.VAPID_PUBLIC_KEY
const linkingNotifyEndpoint = process.env.LINKING_NOTIFY_ENDPOINT
const linkingNotifyToken = process.env.LINKING_NOTIFY_TOKEN
const dappOfferUrl = process.env.DAPP_OFFER_URL

if (!privateKey || !publicKey) {
  console.log(
    'Warning: VAPID public or private key not defined, generating one'
  )
  const vapidKeys = webpush.generateVAPIDKeys()
  publicKey = vapidKeys.publicKey
  privateKey = vapidKeys.privateKey
}

webpush.setVapidDetails(`mailto:${emailAddress}`, publicKey, privateKey)

// should be tightened up for security
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept'
  )

  next()
})

// limit request to one per minute
const rateLimiterOptions = {
  points: 1,
  duration: 60
github mozilla / serviceworker-cookbook / push-rich / server.js View on Github external
// Use the web-push library to hide the implementation details of the communication
// between the application server and the push service.
// For details, see https://tools.ietf.org/html/draft-ietf-webpush-protocol and
// https://tools.ietf.org/html/draft-ietf-webpush-encryption.
const webPush = require('web-push');

if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
  console.log("You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY "+
    "environment variables. You can use the following ones:");
  console.log(webPush.generateVAPIDKeys());
  return;
}
// Set the keys used for encrypting the push messages.
webPush.setVapidDetails(
  'https://serviceworke.rs/',
  process.env.VAPID_PUBLIC_KEY,
  process.env.VAPID_PRIVATE_KEY
);

module.exports = function(app, route) {
  app.get(route + 'vapidPublicKey', function(req, res) {
    res.send(process.env.VAPID_PUBLIC_KEY);
  });

  app.post(route + 'register', function(req, res) {
    // A real world application would store the subscription info.
github mozilla / serviceworker-cookbook / push-quota / server.js View on Github external
// Use the web-push library to hide the implementation details of the communication
// between the application server and the push service.
// For details, see https://tools.ietf.org/html/draft-ietf-webpush-protocol and
// https://tools.ietf.org/html/draft-ietf-webpush-encryption.
const webPush = require('web-push');

if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
  console.log("You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY "+
    "environment variables. You can use the following ones:");
  console.log(webPush.generateVAPIDKeys());
  return;
}
// Set the keys used for encrypting the push messages.
webPush.setVapidDetails(
  'https://serviceworke.rs/',
  process.env.VAPID_PUBLIC_KEY,
  process.env.VAPID_PRIVATE_KEY
);

module.exports = function(app, route) {
  app.get(route + 'vapidPublicKey', function(req, res) {
    res.send(process.env.VAPID_PUBLIC_KEY);
  });

  app.post(route + 'register', function(req, res) {
    // A real world application would store the subscription info.
github mozilla / serviceworker-cookbook / push-clients / server.js View on Github external
// Use the web-push library to hide the implementation details of the communication
// between the application server and the push service.
// For details, see https://tools.ietf.org/html/draft-ietf-webpush-protocol and
// https://tools.ietf.org/html/draft-ietf-webpush-encryption.
const webPush = require('web-push');

if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
  console.log("You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY "+
    "environment variables. You can use the following ones:");
  console.log(webPush.generateVAPIDKeys());
  return;
}
// Set the keys used for encrypting the push messages.
webPush.setVapidDetails(
  'https://serviceworke.rs/',
  process.env.VAPID_PUBLIC_KEY,
  process.env.VAPID_PRIVATE_KEY
);

module.exports = function(app, route) {
  app.get(route + 'vapidPublicKey', function(req, res) {
    res.send(process.env.VAPID_PUBLIC_KEY);
  });

  app.post(route + 'register', function(req, res) {
    // A real world application would store the subscription info.

web-push

Web Push library for Node.js

MPL-2.0
Latest version published 10 months ago

Package Health Score

69 / 100
Full package analysis