Created
June 29, 2012 23:56
-
-
Save astanway/3021501 to your computer and use it in GitHub Desktop.
node pubsub
This file contains 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
# NOTE: Requires https://github.com/technoweenie/nubnub | |
Client = require '../src/client' | |
cli = Client.build( | |
hub: "http://hub.etsy.com/" | |
mode: "subscribe" | |
verify: 'sync' | |
topic: 'http://www.etsy.com/api/push/listings/latest.atom' | |
callback: 'http://ec2-23-20-144-147.compute-1.amazonaws.com:8080/callback' | |
apikey: 'YOURKEYHERE' | |
) | |
console.log "subscribing..." | |
cli.subscribe (err, resp, body) -> | |
if err | |
console.log err | |
console.log "#{resp.statusCode}: #{body}" |
This file contains 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 url = require('url') | |
var events = require('events').EventEmitter | |
events = new events() | |
var port = 8080 | |
server = http.createServer(function(req, resp){ | |
var body = '' | |
req.on('data', function(chunk){ | |
body += chunk | |
}) | |
req.on('end', function(){ | |
if (req.method == 'GET'){ | |
challenge = gup('hub.challenge', req.url) | |
resp.writeHead(200) | |
resp.write(challenge) | |
} else { | |
resp.writeHead(200) | |
events.emit('publish', req.headers['content-type'], body) | |
} | |
resp.end() | |
}) | |
}) | |
server.listen(port, function(){ | |
console.log("listening") | |
}) | |
events.on('publish', function (contentType, data) { | |
console.log(data) | |
}) | |
function gup(param, req){ | |
param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); | |
var regexS = "[\\?&]" + param + "=([^&#]*)"; | |
var regex = new RegExp( regexS ); | |
var results = regex.exec(req); | |
if( results == null ) | |
return ""; | |
else | |
return results[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment