-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify-example.lua
More file actions
34 lines (30 loc) · 796 Bytes
/
notify-example.lua
File metadata and controls
34 lines (30 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
local cqueues = require "cqueues"
local pgsql = require "cqueues_pgsql"
local conn = pgsql.connectdb ""
if conn:status() ~= pgsql.CONNECTION_OK then
error(conn:errorMessage(), nil)
end
assert(conn:exec("LISTEN somechannel"):status() == pgsql.PGRES_COMMAND_OK)
local loop = cqueues.new()
loop:wrap(function()
while true do
if cqueues.poll({pollfd = conn:socket(); events = "r"}) then
conn:consumeInput()
local n = conn:notifies()
if n then
print("NOTIFIED", n:relname(), n:pid(), n:extra())
end
end
end
end)
loop:wrap(function()
while true do
local res = conn:exec("NOTIFY somechannel, 'hi!'")
if not res or res:status() ~= pgsql.PGRES_COMMAND_OK then
error(conn:errorMessage(), nil)
end
print("SENT NOTIFY")
cqueues.sleep(2)
end
end)
assert(loop:loop())