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

byteskode-push

v0.1.1

Published

byteskode push notifiation with mongoose persistence and kue support

Downloads

3

Readme

byteskode-push

Build Status

byteskode push notifiation with mongoose persistence and kue support

Note: byteskode-push is configured using config using key push

Requirements

Installation

$ npm install --save mongoose byteskode-push

Usage

var mongoose = require('mongoose');
var PushNotification = require('byteskode-push');

//push notification
var pushNotification = {
        to: faker.random.uuid(),
        data: {
            key1: 'message1',
            key2: 'message2'
        },
        notification: {
            title: 'Alert!!!',
            body: 'Abnormal data access',
            icon: 'ic_launcher'
        },
        options: {
            collapseKey: 'demo',
            priority: 'high',
            contentAvailable: true,
            delayWhileIdle: true,
            timeToLive: 3,
            restrictedPackageName: 'somePackageName',
            dryRun: true
        }
    };

//send push notification immediate
PushNotification.send(pushNotification, function(error, _pushNotification){
            ...
});

//queue for later sending
//you will have to implement worker for later resend
//or use built in kue worker and queue
PushNotification.queue(pushNotification);

API

send(pushNotification:Object,[options:Object], callback(error, pushNotification))

Before send all push notifications are persisted to mongodb using mongoose. pushNotification object should constitute valid node-gcm message and its options.

If running in production and still want to simulate fake sent pass an additional object with fake key set to true.

To pass node-gcm send options such as retries and backoff strategy also pass them on the options object.

Example

//real send
PushNotification.send(pushNotification, function(error, pushNotification){
            ...
    });

//simulate send
PushNotification.send(pushNotification, {fake:true}, function(error, pushNotification){
            ...
    });

resend([criteria:Object], callback(error, pushNotifications))

Resend will try to resend failed push notifications that are in the database. criteria is a valid mongoose criteria used to specify which failed push notifications to resend.

Example

PushNotification.resend(fuction(error, pushNotifications){
    ...
});

//or pass criteria
PushNotification.resend(criteria, fuction(error, pushNotifications){
    ...
});

queue(pushNotification:Object,[options:Object], [callback(error, pushNotification)])

Unlike send, queue will save push notification for later processing. After push notification persisted into database mail:queued event will be fired with an instance of saved push notification. If any error occur an event mail:queue:error will be fired.

Example

PushNotification.on('push:queued', fuction(pushNotification){
    ...
    //process pushNotification in background or real queue like kue
    ...
});

PushNotification.on('push:queue:error', fuction(error){
   ...
   //handle error
   ... 
});

//queue push notification
PushNotification.queue(pushNotification);

requeue([criteria:Object], [callback(error, pushNotification)])

Unlike resend, requeue will fire push:queued event on every unsent push notification.

Example

PushNotification.requeue();

//or pass criteria
PushNotification.requeue(criteria);

Configuration Options

Base on your environment setup, ensure you have the following configurations in your config files.

push: {
    //sender GCM(FCM) API key
    //@see {@link https://github.com/ToothlessGear/node-gcm#requirements}
    //@see {@link https://developers.google.com/cloud-messaging/gcm#apikey}
    apiKey: <your_api_key>,

    //custom GCM(FCM) request options to be passed to node-gcm request
    //@see {@link https://github.com/ToothlessGear/node-gcm#custom-gcm-request-options}
    requestOptions: {},

    //valid node-gcm send options
    //used to specify number of retries and back-off strategy
    //@see {@link https://github.com/ToothlessGear/node-gcm/blob/master/lib/sender.js#L15}
    sendOptions: {},

    //custom options to be merged
    //on PushNotification schema and model
    model: {

        //name of the model to be used when register to mongoose
        //default to PushNotification
        name: 'PushNotification',

        //additional custom schema fields to add on the push notification schema
        fields: {}
    },

    //allow log of push notification after send
    debug: false,

    //logger to be used
    //use console log as default logger
    logger: console.log,

    //kue options
    //if available kue queue will be created
    //and worker to process push notification in worker
    //process will be available
    //see below for details
    kue: {
            concurrency: 10,
            timeout: 5000,
            queue: 'push:queued',
            connection: {}
        }
    }

Kue Integration

To add support to kue ensure you have installed kue and supply the required configuration. The presence of kue configuration in push config options will signal the use of kue publisher and worker. See Example

$ npm install --save kue

Kue Push Worker

In your worker process start the queued push worker as below

var mongoose = require('mongoose');
var PushNotification = require('byteskode-push');

//Alert!: Probably your should start push notification
//processing in your worker process
//and not main process
PushNotification.worker.start();

//listen for the worker queue events
PushNotifcation.worker.queue.on('job complete', function(id, result) {
    console.log('complete', id, result);
});

PushNotifcation.worker.queue.on('error', function(error) {
    console.log('error', error);
});

//anywhere in your main process
//queue push notification for background send
PushNotification.queue(pushNotification);

Testing

  • Clone this repository

  • Install all development dependencies

$ npm install
  • Then run test
$ npm test

Contribute

It will be nice, if you open an issue first so that we can know what is going on, then, fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.

Licence

The MIT License (MIT)

Copyright (c) 2015 byteskode, lykmapipo & Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.