Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created July 2, 2012 21:25
Show Gist options
  • Save robertsosinski/3035806 to your computer and use it in GitHub Desktop.
Save robertsosinski/3035806 to your computer and use it in GitHub Desktop.
PostgreSQL Listen/Notifiy
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
@ngauthier
Copy link

Did you get this to work? My Subscriber never fires, even when there are notifications to read.

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