3 require 'faye/websocket'
7 @channel = EventMachine::Channel.new
12 sub = @channel.subscribe do |msg|
14 ws.send({:message => "log"}.to_json)
17 ws.on :message do |event|
18 puts "got #{event.data}"
22 ws.on :close do |event|
23 p [:close, event.code, event.reason]
24 @channel.unsubscribe sub
31 # from http://stackoverflow.com/questions/16405520/postgres-listen-notify-rails
32 ActiveRecord::Base.connection_pool.with_connection do |connection|
33 conn = connection.instance_variable_get(:@connection)
35 conn.async_exec "LISTEN logs"
37 conn.wait_for_notify do |channel, pid, payload|
38 puts "Received a NOTIFY on channel #{channel}"
39 puts "from PG backend #{pid}"
40 puts "saying #{payload}"
45 # Don't want the connection to still be listening once we return
46 # it to the pool - could result in weird behavior for the next
47 # thread to check it out.
48 conn.async_exec "UNLISTEN *"