PushJet API
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
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
uuid | string | The device UUID | D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 | X |
markMessagesAsRead(uuid)
Mark messages as read
Parameters
uuid | string | The device UUID | D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 | X |
createService(name, icon)
Create service
Parameters
getServiceInfo(service, secret)
Get service info
Parameters
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
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
secret | string | The service secret | d2d1820d56b862a6f5b1a69a7af730fa | X |
subscribeToService(uuid, service)
Subscribe to a service
Parameters
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
uuid | string | The device UUID | D867AB3E-36D2-11E4-AEA8-76C9E2E253B6 | X |
unsubscribe(uuid, servie)
Unsubscribe
Parameters
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
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
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';
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);
});
};
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) {
setTimeout(push, 1000);
return;
}
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(pusher, service.public);
push();
}).catch((error) => {
console.log('error', error);
});
Third-party libraries