Created
April 1, 2015 19:04
-
-
Save pke/5f6e03ff741478890f97 to your computer and use it in GitHub Desktop.
Serving Trello notifications to pubnub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var pubnub = require("pubnub")({ | |
ssl : true, // <- enable TLS Tunneling over TCP | |
publish_key: 'pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
subscribe_key: 'sub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
}); | |
var port = process.env.port || 1337; | |
http.createServer(function (req, res) { | |
if (req.method === "POST") { | |
var jsonString = ''; | |
req.on('data', function (data) { | |
jsonString += data; | |
}); | |
req.on('end', function (data) { | |
pubnub.publish({ | |
channel : req.url.substr(1), | |
message : JSON.parse(jsonString), | |
callback : function (e) { console.log("SUCCESS!", e); }, | |
error : function (e) { console.log("FAILED! RETRY PUBLISH!", e); } | |
}); | |
res.writeHead(200, "OK", { 'Content-Type': 'text/html' }); | |
res.end(); | |
}); | |
} else if (req.method === "HEAD") { | |
res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
res.end(); | |
} | |
}).listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment