Created
July 2, 2012 21:25
-
-
Save robertsosinski/3035806 to your computer and use it in GitHub Desktop.
PostgreSQL Listen/Notifiy
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
Did you get this to work? My
Subscriber
never fires, even when there are notifications to read.