Coder Social home page Coder Social logo

pushjet's Introduction

PushJet API

npm version Build status Test coverage Downloads

A Node.js module for using PushJet API.

Installation

npm i pushjet

Usage

All methods described in PushJet documentation are supported.
Each method returns Promise which fulfilled with appropriate json object or rejected with an error.

API

sendMessage(secret, message, title, level, link)

Send a message

Parameters

name type meaning example required
secret string the service secret token d2d1820d56b862a6f5b1a69a7af730fa X
message string The notification text our server is on fire!!@#! X
title string A custom message title Big server #5
level integer The importance level from 1(low) to 5(high) 3
link string http://i.imgur.com/TerUkQY.gif An optional link

fetchUnreadMessages(uuid)

Fetch unread messages

Parameters

name type meaning example required
uuid string The device UUID D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 X

markMessagesAsRead(uuid)

Mark messages as read

Parameters

name type meaning example required
uuid string The device UUID D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 X

createService(name, icon)

Create service

Parameters

name type meaning example required
name string The service name important stuff X
icon string The service icon http://im.gy/images/SkZ.png

getServiceInfo(service, secret)

Get service info

Parameters

name type meaning example required
service string Obtain service info using the public token 4be3-eda97a-0d7faeab05a0-89403-ad4751c49
secret string Obtain service info using the secret d2d1820d56b862a6f5b1a69a7af730fa

updateServiceInfo(secret, name, icon)

Update service info

Parameters

name type meaning example required
secret string The service secret stringd2d1820d56b862a6f5b1a69a7af730fa X
name string Updated service name Cool new name
icon string Updated service image http://im.gy/images/SkZ.png

deleteService(secret)

This will unsubscribe all listeners

Parameters

name type meaning example required
secret string The service secret d2d1820d56b862a6f5b1a69a7af730fa X

subscribeToService(uuid, service)

Subscribe to a service

Parameters

name type meaning example required
uuid string The device UUID D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 X
service string The service's public token 4be3-eda97a-0d7faeab05a0-89403-ad4751c49 X

getSubscriptions(uuid)

Get subscriptions

Parameters

name type meaning example required
uuid string The device UUID D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 X

unsubscribe(uuid, servie)

Unsubscribe

Parameters

name type meaning example required
uuid string The device UUID D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 X
service string The service's public token 4be3-eda97a-0d7faeab05a0-89403-ad4751c49 X

registerDeviceForGCM(uuid, regid, pubkey)

Registering a device for GCM
Only enabled when Google Cloud Messaging is enabled on the server

Parameters

name type meaning example required
uuid string The device UUID D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 X
regid string The registration ID generated by GCM EXAMPLExV2lcV2zEKTLNYs625zfk2jh4EXAMPLE X
pubkey string Optional public key for message encryption

removingGCMRegistration(uuid)

Removing a GCM registration

Parameters

name type meaning example required
uuid string The device UUID D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 X

A quick and dirty example.

We share service's public token between pusher and receiver.

'use strict';

const PushJet = require('pushjet');
const pusher = new PushJet('https://api.pushjet.io/');
const name = 'pizza';
const icon = 'https://ipfs.pics/ipfs/QmVBjUHLS4jewV1VVwDRBfB2DBjqYA993jjUBVez2God21';

// subscribe to a service and receive messages
const subscribe = (pusher, service) => {
  const uuid = require('node-uuid');
  const device = uuid.v4();

  pusher.subscribeToService(device, service).then((subsciption) => {
    console.log('device', device, 'subscribed');

    const  WebSocket = require('ws');
    const ws = new WebSocket('wss://api.pushjet.io/ws');

    ws.on('open', () => {
      console.log('connector connected');
      ws.send(device, (error) => {
        console.log('sending', device, error ? error : 'ok');
      });
    });

    ws.on('message', (data, flags) => {
      console.log(data);
      if (JSON.parse(data).subscription) {
        console.log('connection closed');
        ws.close();
      }
    });
  }).catch((error) => {
    console.log('cannot subscribe', error);
  });
};

// create service, tell about eating pizzas, and then delete service
pusher.createService(name, icon).then((service) => {
  const pizzas = 4;
  let n = 0;

  const push = () => {
    pusher.sendMessage(service.secret, `eat pizza #${++n}`, 'yum-yum')
      .then((status) => {
        console.log('message', n, 'sent', status);
        if (n < pizzas) {
          // schedule next message
          setTimeout(push, 1000);
          return;
        }

        // there are no more pizzas
        pusher.deleteService(service.secret).then((status) => {
          console.log('service deleted');
        }).catch((error) => {
          console.log('cannot delete service', error);
        });
      }).catch((error) => {
        console.log('error', error);
      });
  };

  // subscribe to a service
  subscribe(pusher, service.public);

  // start sending
  push();
}).catch((error) => {
  console.log('error', error);
});

Third-party libraries

License

MIT

pushjet's People

Contributors

tsypa avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.