2016-11-06 21:26:19 +01:00
|
|
|
|
2023-02-02 11:57:05 +01:00
|
|
|
module.exports = function (RED) {
|
2014-06-29 00:34:59 +02:00
|
|
|
"use strict";
|
2023-02-02 11:57:05 +01:00
|
|
|
const Pusher = require("pusher")
|
|
|
|
const PusherClient = require('pusher-js');
|
2014-06-29 00:34:59 +02:00
|
|
|
|
|
|
|
//node for subscribing to an event/channel
|
2015-05-11 20:25:39 +02:00
|
|
|
function PusherNode(n) {
|
2023-02-02 11:57:05 +01:00
|
|
|
RED.nodes.createNode(this, n);
|
2017-08-27 15:00:20 +02:00
|
|
|
this.channel = n.channel;
|
|
|
|
this.eventname = n.eventname;
|
|
|
|
this.cluster = n.cluster || "mt1";
|
2014-06-29 00:34:59 +02:00
|
|
|
var node = this;
|
2015-02-06 22:10:14 +01:00
|
|
|
var credentials = this.credentials;
|
2014-06-29 00:34:59 +02:00
|
|
|
|
2017-08-27 15:00:20 +02:00
|
|
|
if ((credentials) && (credentials.hasOwnProperty("pusherappkeysub"))) {
|
|
|
|
node.appkey = credentials.pusherappkeysub;
|
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
else { this.error("No Pusher app key set for input node"); }
|
|
|
|
|
|
|
|
//create a subscription to the channel and event defined by user
|
2023-02-02 11:57:05 +01:00
|
|
|
// var socket = new PusherClient(''+node.appkey, {cluster:node.cluster, encrypted:true});
|
|
|
|
const pusher = new PusherClient('' + node.appkey, {
|
|
|
|
cluster: node.cluster
|
|
|
|
});
|
|
|
|
const channel = pusher.subscribe('' + node.channel);
|
|
|
|
|
|
|
|
channel.bind('' + node.eventname, function (data) {
|
|
|
|
var msg = { topic: node.eventname, channel: node.channel };
|
|
|
|
if (data.hasOwnProperty("payload")) { msg.payload = data.payload; }
|
|
|
|
else { msg.payload = data; }
|
|
|
|
node.send(msg);
|
|
|
|
});
|
|
|
|
|
|
|
|
node.on("close", function () {
|
|
|
|
pusher.disconnect();
|
2014-06-29 00:34:59 +02:00
|
|
|
});
|
|
|
|
}
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2017-08-27 15:00:20 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
//Node for sending Pusher events
|
|
|
|
function PusherNodeSend(n) {
|
|
|
|
// Create a RED node
|
2023-02-02 11:57:05 +01:00
|
|
|
RED.nodes.createNode(this, n);
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
var node = this;
|
2015-02-06 22:10:14 +01:00
|
|
|
var credentials = this.credentials;
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
if ((credentials) && (credentials.hasOwnProperty("pusherappid"))) { this.appid = credentials.pusherappid; }
|
|
|
|
else { this.error("No Pusher api token set"); }
|
|
|
|
if ((credentials) && (credentials.hasOwnProperty("pusherappsecret"))) { this.appsecret = credentials.pusherappsecret; }
|
|
|
|
else { this.error("No Pusher user secret set"); }
|
|
|
|
if ((credentials) && (credentials.hasOwnProperty("pusherappkey"))) { this.appkey = credentials.pusherappkey; }
|
|
|
|
else { this.error("No Pusher user key set"); }
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
//get parameters from user
|
|
|
|
this.channel = n.channel;
|
|
|
|
this.eventname = n.eventname;
|
2017-08-27 15:00:20 +02:00
|
|
|
this.cluster = n.cluster || "mt1";
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2023-02-02 11:57:05 +01:00
|
|
|
var pusher = new Pusher({
|
2014-06-29 00:34:59 +02:00
|
|
|
appId: this.appid,
|
|
|
|
key: this.appkey,
|
2017-08-27 15:00:20 +02:00
|
|
|
secret: this.appsecret,
|
|
|
|
cluster: this.cluster
|
2014-05-18 23:10:00 +02:00
|
|
|
});
|
|
|
|
|
2023-02-02 11:57:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
node.on("input", function (msg) {
|
|
|
|
pusher.trigger(this.channel, this.eventname, {
|
2014-06-29 00:34:59 +02:00
|
|
|
"payload": msg.payload
|
|
|
|
});
|
|
|
|
});
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2023-02-02 11:57:05 +01:00
|
|
|
node.on("close", function () {
|
2014-06-29 00:34:59 +02:00
|
|
|
});
|
2015-05-11 20:25:39 +02:00
|
|
|
}
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2023-02-02 11:57:05 +01:00
|
|
|
RED.nodes.registerType("pusher in", PusherNode, {
|
2015-02-06 22:10:14 +01:00
|
|
|
credentials: {
|
2017-08-27 15:00:20 +02:00
|
|
|
pusherappkeysub: "text"
|
2015-04-24 22:17:02 +02:00
|
|
|
}
|
2014-05-18 23:10:00 +02:00
|
|
|
});
|
2023-02-02 11:57:05 +01:00
|
|
|
RED.nodes.registerType("pusher out", PusherNodeSend, {
|
2015-02-06 22:10:14 +01:00
|
|
|
credentials: {
|
2023-02-02 11:57:05 +01:00
|
|
|
pusherappid: { type: "text" },
|
|
|
|
pusherappkey: { type: "text" },
|
|
|
|
pusherappsecret: { type: "password" }
|
2017-08-27 15:00:20 +02:00
|
|
|
},
|
|
|
|
encrypted: true
|
2014-06-29 00:34:59 +02:00
|
|
|
});
|
|
|
|
}
|