Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About redis:subscribe #64

Closed
wy-z opened this issue Aug 29, 2015 · 4 comments
Closed

About redis:subscribe #64

wy-z opened this issue Aug 29, 2015 · 4 comments

Comments

@wy-z
Copy link

wy-z commented Aug 29, 2015

Thanks so much for your great contribution. @agentzh
And there is a problem with redis:subscribe.
Can I subscribe multiple channels by such code?

redis:subscribe('channel:first', 'channel:second')
or
redis:subscribe({'channel:first', 'channel:second'})
@agentzh
Copy link
Member

agentzh commented Aug 29, 2015

@weiyoung-zhou According to the official redis command documentation below

http://redis.io/commands/subscribe

your first usage is correct.

@wy-z
Copy link
Author

wy-z commented Aug 30, 2015

The problem has been solved.

Redis-cli subscribe response

input: 
    subscribe channel:first channel:second
response:
    1) "subscribe"
    2) "channel:first"
    3) (integer) 1
    1) "subscribe"
    2) "channel:second"
    3) (integer) 2

The second response

    1) "subscribe"
    2) "channel:second"
    3) (integer) 2

will be returned by sub:read_reply.

Thanks for your attention.


Thanks for your response.Your attention to this problem will be highly appreciated. @agentzh
It seems that redis:subscribe('channel:first', 'channel:second') will cause some error, but I am not quit sure.
Here is the code.


local redis = require "resty.redis"
local cjson = require "cjson"

local user_uid = ngx.var.user_uid
local user_channel = "channel:user:"..user_uid

local server = require "resty.websocket.server"
local wb, err = server:new{
    timeout = 5000,
    max_payload_len = 65535
}
if not wb then
    ngx.log(ngx.ERR, "failed to new websocket: ", err)
    return ngx.exit(444)
end

local function subscribe (ws)

    local sub = redis:new()
    sub:connect("127.0.0.1", 6379)
    local res, err = sub:subscribe(user_channel)
    if not res then
        ngx.say("failed to subscribe: ", err)
        return
    end

    while true do
        local bytes, err = sub:read_reply()
        if bytes then
            local _json = cjson.decode(bytes[3])
            if _json.type == 'subscribe' then
                -- for k, v in pairs(_json.data) do
                local res, err = sub:subscribe('channel:first', 'channel:second')
                if err then
                    ws:send_text("{'type': 'ws_response', 'data': 0}")
                    ws:send_text(err)
                end
                -- end
            elseif _json.type == 'unsubscribe' then
                sub:unsubscribe()
                break
            else
                ws:send_text(bytes[3])
            end
        else
            ngx.log(ngx.ERR, "no data")
        end
    end
end

ngx.thread.spawn(subscribe, wb)

local pub = redis:new()
pub:connect("127.0.0.1", 6379)

while true do 
    local data, typ, err = wb:recv_frame()
    if wb.fatal then
        return ngx.exit(444)
    end
    if not data then
        local bytes, err = wb:send_ping()
        if not bytes then
            return ngx.exit(444)
        end

    elseif typ == "close" then
        break
    elseif typ == "ping" then
        local bytes, err = wb:send_pong()
        if not bytes then
            return ngx.exit(444)
        end
    elseif typ == "pong" then
        ngx.log(ngx.INFO, "client ponged")

    elseif typ == "text" then
        pub:publish(user_channel, data)
    end
end
wb:send_close()

Stack traceback

2015/08/30 14:49:45 [error] 1606#0: *519 lua user thread aborted: runtime error: /Users/ones/ones/hua_sheng/lua/ws_notify.lua:39: attempt to index local '_json' (a number value)
stack traceback:
coroutine 0:
    /Users/ones/ones/hua_sheng/lua/ws_notify.lua: in function </Users/ones/ones/hua_sheng/lua/ws_notify.lua:25>
coroutine 1:
    [C]: in function 'receive'
    /usr/local/openresty/lualib/resty/websocket/protocol.lua:48: in function '_recv_frame'
    /usr/local/openresty/lualib/resty/websocket/server.lua:149: in function 'recv_frame'
    /Users/ones/ones/hua_sheng/lua/ws_notify.lua:65: in function </Users/ones/ones/hua_sheng/lua/ws_notify.lua:1>, client: 127.0.0.1, server: , request: "GET /ws/notify/31 HTTP/1.1", host: "127.0.0.1"

Redis-cli subscribe response

input: 
    subscribe channel:first channel:second
response:
    1) "subscribe"
    2) "channel:first"
    3) (integer) 1
    1) "subscribe"
    2) "channel:second"
    3) (integer) 2

Lua-resty-redis subscribe response

input:
    subscribe('channel:first', 'channel:second')
response:
    1) "subscribe"
    2) "channel:first"
    3) (integer) 1

@agentzh
Copy link
Member

agentzh commented Aug 31, 2015

@weiyoung-zhou I guess there's some bug in your own Lua code. Try starting with the simplest case to isolate the problem.

If you really believe it is a bug in lua-resty-redis, please try to provide a standalone and minimized example that I can run on my side to reproduce the issue. The code snippet in your last comment is just too large for me to analyze.

@wy-z
Copy link
Author

wy-z commented Aug 31, 2015

@agentzh The problem has been solved.
Thanks for your attention.

@wy-z wy-z closed this as completed Dec 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants