2014-05-18 23:10:00 +02:00
|
|
|
/**
|
2014-05-24 17:38:35 +02:00
|
|
|
* pusher.js
|
|
|
|
* Subscription module for the Pusher service (www.pusher.com)
|
|
|
|
* Requires 'pusher' and 'pusher-client' modules.
|
|
|
|
*
|
2015-02-06 22:10:14 +01:00
|
|
|
* Copyright 2014, 2015 Charalampos Doukas, @BuildingIoT
|
2014-05-24 17:38:35 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
2014-06-29 00:34:59 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var Pusher = require('pusher');
|
|
|
|
var PusherClient = require('pusher-client');
|
|
|
|
|
|
|
|
//node for subscribing to an event/channel
|
2015-05-11 20:25:39 +02:00
|
|
|
function PusherNode(n) {
|
2014-06-29 00:34:59 +02:00
|
|
|
// Create a RED node
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
|
|
|
|
var node = this;
|
2015-02-06 22:10:14 +01:00
|
|
|
var credentials = this.credentials;
|
2014-06-29 00:34:59 +02:00
|
|
|
|
|
|
|
if ((credentials) && (credentials.hasOwnProperty("pusherappkey_sub"))) { this.appkey = credentials.pusherappkey_sub; }
|
|
|
|
else { this.error("No Pusher app key set for input node"); }
|
|
|
|
|
|
|
|
//get parameters from user
|
|
|
|
this.channel = n.channel;
|
|
|
|
this.eventname = n.eventname;
|
|
|
|
|
|
|
|
//create a subscription to the channel and event defined by user
|
|
|
|
var socket = new PusherClient(''+this.appkey);
|
|
|
|
var my_channel = socket.subscribe(''+this.channel);
|
|
|
|
socket.bind(''+this.eventname,
|
|
|
|
function(data) {
|
2015-04-24 22:17:02 +02:00
|
|
|
var msg = {topic:this.eventname};
|
2014-06-29 00:34:59 +02:00
|
|
|
if (data.hasOwnProperty("payload")) { msg.payload = data.payload; }
|
|
|
|
else { msg.payload = data; }
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.on("close", function() {
|
|
|
|
socket.disconnect();
|
|
|
|
});
|
|
|
|
}
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
//Node for sending Pusher events
|
|
|
|
function PusherNodeSend(n) {
|
|
|
|
// Create a RED node
|
|
|
|
RED.nodes.createNode(this,n);
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
var node = this;
|
2014-05-18 23:10:00 +02:00
|
|
|
|
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;
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
var pusher = new Pusher({
|
|
|
|
appId: this.appid,
|
|
|
|
key: this.appkey,
|
|
|
|
secret: this.appsecret
|
2014-05-18 23:10:00 +02:00
|
|
|
});
|
|
|
|
|
2015-04-24 22:17:02 +02:00
|
|
|
this.on("input", function(msg) {
|
2014-06-29 00:34:59 +02:00
|
|
|
pusher.trigger(this.channel, this.eventname, {
|
|
|
|
"payload": msg.payload
|
|
|
|
});
|
|
|
|
});
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
this.on("close", function() {
|
|
|
|
});
|
2015-05-11 20:25:39 +02:00
|
|
|
}
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
//debugging on the output:
|
2014-06-29 00:34:59 +02:00
|
|
|
var displayResult = function(result) {
|
|
|
|
node.log(result);
|
|
|
|
};
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
var displayError = function(err) {
|
|
|
|
node.log("Error: "+err);
|
|
|
|
};
|
2014-05-18 23:10:00 +02:00
|
|
|
|
2015-02-06 22:10:14 +01:00
|
|
|
RED.nodes.registerType("pusher in",PusherNode,{
|
|
|
|
credentials: {
|
|
|
|
pusherappkey_sub: "text"
|
2015-04-24 22:17:02 +02:00
|
|
|
}
|
2014-05-18 23:10:00 +02:00
|
|
|
});
|
2015-02-06 22:10:14 +01:00
|
|
|
RED.nodes.registerType("pusher out",PusherNodeSend,{
|
|
|
|
credentials: {
|
|
|
|
pusherappid: {type:"text"},
|
|
|
|
pusherappkey: {type:"text"},
|
|
|
|
pusherappsecret: {type:"password"}
|
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
});
|
|
|
|
}
|