-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathfirebase_watch.js
More file actions
27 lines (22 loc) · 826 Bytes
/
firebase_watch.js
File metadata and controls
27 lines (22 loc) · 826 Bytes
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
module.exports = function(RED) {
"use strict";
var Firebase = require("firebase");
function sendMessageFromSnapshot(snapshot) {
var msg = {};
msg.href = snapshot.ref().toString();
msg.payload = snapshot.val();
this.send(msg);
}
function FirebaseWatch(n) {
RED.nodes.createNode(this,n);
this.firebaseurl = n.firebaseurl;
this.firebase = new Firebase(this.firebaseurl);
this.onValue = sendMessageFromSnapshot.bind(this);
this.firebase.on("value", this.onValue);
this.on('close', function() {
// We need to unbind our callback, or we'll get duplicate messages when we redeploy
this.firebase.off("value", this.onValue);
});
}
RED.nodes.registerType("firebase watch", FirebaseWatch);
}