Created
April 9, 2010 00:43
-
-
Save davidjrice/360733 to your computer and use it in GitHub Desktop.
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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