-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathpubsub.lua
More file actions
36 lines (30 loc) · 1.12 KB
/
Copy pathpubsub.lua
File metadata and controls
36 lines (30 loc) · 1.12 KB
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
35
36
package.path = "../src/?.lua;src/?.lua;" .. package.path
pcall(require, "luarocks.require")
local redis = require 'redis'
local params = {
host = '127.0.0.1',
port = 6379,
}
local client = redis.connect(params)
client:select(15) -- for testing purposes
local channels = { 'control_channel', 'notifications' }
-- Start processing the pubsup messages. Open a terminal and use redis-cli
-- to push messages to the channels. Examples:
-- ./redis-cli PUBLISH notifications "this is a test"
-- ./redis-cli PUBLISH control_channel quit_loop
for msg, abort in client:pubsub({ subscribe = channels }) do
if msg.kind == 'subscribe' then
print('Subscribed to channel '..msg.channel)
elseif msg.kind == 'message' then
if msg.channel == 'control_channel' then
if msg.payload == 'quit_loop' then
print('Aborting pubsub loop...')
abort()
else
print('Received an unrecognized command: '..msg.payload)
end
else
print('Received the following message from '..msg.channel.."\n "..msg.payload.."\n")
end
end
end