Giter Club home page Giter Club logo

Comments (4)

agentzh avatar agentzh commented on July 29, 2024

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

http://redis.io/commands/subscribe

your first usage is correct.

from lua-resty-redis.

wy-z avatar wy-z commented on July 29, 2024

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

from lua-resty-redis.

agentzh avatar agentzh commented on July 29, 2024

@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.

from lua-resty-redis.

wy-z avatar wy-z commented on July 29, 2024

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

from lua-resty-redis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.