Skip to content

Instantly share code, notes, and snippets.

@astanway
Created June 29, 2012 23:56
Show Gist options
  • Save astanway/3021501 to your computer and use it in GitHub Desktop.
Save astanway/3021501 to your computer and use it in GitHub Desktop.
node pubsub
# 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}"
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