-
Notifications
You must be signed in to change notification settings - Fork 594
/
pusher.js
93 lines (76 loc) · 3.01 KB
/
pusher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module.exports = function (RED) {
"use strict";
const Pusher = require("pusher")
const PusherClient = require('pusher-js');
//node for subscribing to an event/channel
function PusherNode(n) {
RED.nodes.createNode(this, n);
this.channel = n.channel;
this.eventname = n.eventname;
this.cluster = n.cluster || "mt1";
var node = this;
var credentials = this.credentials;
if ((credentials) && (credentials.hasOwnProperty("pusherappkeysub"))) {
node.appkey = credentials.pusherappkeysub;
}
else { this.error("No Pusher app key set for input node"); }
//create a subscription to the channel and event defined by user
// 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();
});
}
//Node for sending Pusher events
function PusherNodeSend(n) {
// Create a RED node
RED.nodes.createNode(this, n);
var node = this;
var credentials = this.credentials;
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"); }
//get parameters from user
this.channel = n.channel;
this.eventname = n.eventname;
this.cluster = n.cluster || "mt1";
var pusher = new Pusher({
appId: this.appid,
key: this.appkey,
secret: this.appsecret,
cluster: this.cluster
});
node.on("input", function (msg) {
pusher.trigger(this.channel, this.eventname, {
"payload": msg.payload
});
});
node.on("close", function () {
});
}
RED.nodes.registerType("pusher in", PusherNode, {
credentials: {
pusherappkeysub: "text"
}
});
RED.nodes.registerType("pusher out", PusherNodeSend, {
credentials: {
pusherappid: { type: "text" },
pusherappkey: { type: "text" },
pusherappsecret: { type: "password" }
},
encrypted: true
});
}