Skip to content

Commit

Permalink
examples/pubsub: Add pubsub example
Browse files Browse the repository at this point in the history
  • Loading branch information
daurnimator committed Dec 21, 2015
1 parent decbf5c commit 5d9189f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/pubsub.lua
@@ -0,0 +1,34 @@
local lrc = require "lredis.cqueues"
local cqueues = require "cqueues"
-- Make a new cqueues scheduler
local cq = cqueues.new()
-- Make a thread that prints published messages to stdout
cq:wrap(function()
local r = lrc.connect_tcp()
r:subscribe("quit")
r:psubscribe("b*")
while true do
local item = r:get_next()
if item == nil then break end
-- Can write `for item in r.get_next, r do` instead
-- but that doesn't work in lua5.1/luajit
local message_type = item[1]
if message_type == "message" then
print("Channel:", item[2], "Message:", item[3])
if item[2] == "quit" then break end
elseif message_type == "pmessage" then
print("Channel:", item[3], "Message:", item[4])
end
end
end)
-- Make a second thread that publishes events on an interval
cq:wrap(function()
local r = lrc.connect_tcp()
for i=1, 10 do
cqueues.sleep(0.2)
r:call("publish", "bar", tostring(i))
end
r:call("publish", "quit", "")
end)
-- Start 'main' loop
assert(cq:loop())

0 comments on commit 5d9189f

Please sign in to comment.