Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created May 14, 2012 04:37
Show Gist options
  • Save robertsosinski/2691813 to your computer and use it in GitHub Desktop.
Save robertsosinski/2691813 to your computer and use it in GitHub Desktop.
Testing Postgres Listen/Notify using EventMachine
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