-
-
Save carld/f8977d6244eebd0c9ff51ac6c871b268 to your computer and use it in GitHub Desktop.
Testing Postgres Listen/Notify using EventMachine
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
require 'rubygems' | |
require 'pg' | |
require 'eventmachine' | |
module Subscriber | |
def initialize(pg) | |
@pg = pg | |
end | |
def notify_readable | |
@pg.consume_input | |
while notification = @pg.notifies | |
puts notification.inspect | |
end | |
end | |
def unbind | |
@pg.close | |
end | |
end | |
pg = PGconn.connect(:dbname => 'postgres') | |
pg.exec "set application_name = 'test'" | |
pg.exec "LISTEN test" | |
EM.run do | |
EM.watch(pg.socket, Subscriber, pg) { |c| c.notify_readable = true } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment