Last active
November 29, 2017 03:16
-
-
Save dux/0e0b111a6363e4306332e24807ebe220 to your computer and use it in GitHub Desktop.
Postgres LISTEN and NOTIFY with payload example
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 'sequel' | |
require 'securerandom' | |
# DB = Sequel.postgres(...) | |
ch_name = :test_1 | |
Thread.new do | |
DB.listen(ch_name, loop: true) do |channel, pid, payload| | |
p [channel, pid, payload] | |
end | |
end | |
loop do | |
DB.notify ch_name, payload: SecureRandom.hex(10) | |
sleep rand | |
end | |
# ["test1", 12595, "cbbfed1518740b31a75e"] | |
# ["test1", 12595, "450152f326386bcb2c20"] | |
# ["test1", 12595, "beb87cdef62650ee48e8"] | |
# ["test1", 12595, "9159323c83746d133ee5"] | |
# ... | |
# update row and return it | |
# DB.fetch('update table_name set active=true where id = (select id from table_name where active=false limit 1) returning *').all[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment