File: pubsub.lua

package info (click to toggle)
lua-redis 2.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 264 kB
  • ctags: 126
  • sloc: makefile: 2
file content (36 lines) | stat: -rw-r--r-- 1,150 bytes parent folder | download | duplicates (5)
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