Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidjrice/360733 to your computer and use it in GitHub Desktop.
Save davidjrice/360733 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
require 'em-http'
require 'json'
URL = 'http://localhost:5984/comments/_changes?feed=continuous'
def handle_change(change)
puts "#{change['seq']}: #{change['id']}"
end
def monitor_couch
EventMachine.run do
http = EventMachine::HttpRequest.new(URL).get :timeout => 0
buffer = ""
http.errback { puts "fail" }
http.callback {
monitor_couch
}
http.stream do |chunk|
buffer += chunk
while line = buffer.slice!(/.+\r?\n/)
begin
handle_change JSON.parse(line)
rescue
puts "Invalid JSON: #{line}"
end
end
end
end
end
monitor_couch
@nicogranelli
Copy link

http.callback {
  monitor_couch  
}

What this code does is restart the EM if couch stop the connection for any reason, right?

Thanks for this gist BTW, the only concise example I was able to find about getting the changes feed of couchdb with EM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment