0.0.3 • Published 8 years ago

pushjet v0.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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

nametypemeaningexamplerequired
secretstringthe service secret tokend2d1820d56b862a6f5b1a69a7af730faX
messagestringThe notification textour server is on fire!!@#!X
titlestringA custom message titleBig server #5
levelintegerThe importance level from 1(low) to 5(high)3
linkstringhttp://i.imgur.com/TerUkQY.gifAn optional link

fetchUnreadMessages(uuid)

Fetch unread messages

Parameters

nametypemeaningexamplerequired
uuidstringThe device UUIDD867AB3E-36D2-11E4-AEA8-76C9E2E253B6X

markMessagesAsRead(uuid)

Mark messages as read

Parameters

nametypemeaningexamplerequired
uuidstringThe device UUIDD867AB3E-36D2-11E4-AEA8-76C9E2E253B6X

createService(name, icon)

Create service

Parameters

nametypemeaningexamplerequired
namestringThe service nameimportant stuffX
iconstringThe service iconhttp://im.gy/images/SkZ.png

getServiceInfo(service, secret)

Get service info

Parameters

nametypemeaningexamplerequired
servicestringObtain service info using the public token4be3-eda97a-0d7faeab05a0-89403-ad4751c49
secretstringObtain service info using the secretd2d1820d56b862a6f5b1a69a7af730fa

updateServiceInfo(secret, name, icon)

Update service info

Parameters

nametypemeaningexamplerequired
secretstringThe service secretstringd2d1820d56b862a6f5b1a69a7af730faX
namestringUpdated service nameCool new name
iconstringUpdated service imagehttp://im.gy/images/SkZ.png

deleteService(secret)

This will unsubscribe all listeners

Parameters

nametypemeaningexamplerequired
secretstringThe service secretd2d1820d56b862a6f5b1a69a7af730faX

subscribeToService(uuid, service)

Subscribe to a service

Parameters

nametypemeaningexamplerequired
uuidstringThe device UUIDD867AB3E-36D2-11E4-AEA8-76C9E2E253B6X
servicestringThe service's public token4be3-eda97a-0d7faeab05a0-89403-ad4751c49X

getSubscriptions(uuid)

Get subscriptions

Parameters

nametypemeaningexamplerequired
uuidstringThe device UUIDD867AB3E-36D2-11E4-AEA8-76C9E2E253B6X

unsubscribe(uuid, servie)

Unsubscribe

Parameters

nametypemeaningexamplerequired
uuidstringThe device UUIDD867AB3E-36D2-11E4-AEA8-76C9E2E253B6X
servicestringThe service's public token4be3-eda97a-0d7faeab05a0-89403-ad4751c49X

registerDeviceForGCM(uuid, regid, pubkey)

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

Parameters

nametypemeaningexamplerequired
uuidstringThe device UUIDD867AB3E-36D2-11E4-AEA8-76C9E2E253B6X
regidstringThe registration ID generated by GCMEXAMPLExV2lcV2zEKTLNYs625zfk2jh4EXAMPLEX
pubkeystringOptional public key for message encryption

removingGCMRegistration(uuid)

Removing a GCM registration

Parameters

nametypemeaningexamplerequired
uuidstringThe device UUIDD867AB3E-36D2-11E4-AEA8-76C9E2E253B6X

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