Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save carld/f8977d6244eebd0c9ff51ac6c871b268 to your computer and use it in GitHub Desktop.
Save carld/f8977d6244eebd0c9ff51ac6c871b268 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