Comments (4)
@weiyoung-zhou According to the official redis command documentation below
http://redis.io/commands/subscribe
your first usage is correct.
from lua-resty-redis.
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.
@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.
@agentzh The problem has been solved.
Thanks for your attention.
from lua-resty-redis.
Related Issues (20)
- `register_module_prefix` is documented with `.` but it must be called with `:` HOT 2
- lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/redis.lua:158: bad request HOT 2
- How to connect redisDB with user id and password? HOT 1
- Confusing instructions for installation on README.md
- redis:connect is taking exactly 60 seconds for connection
- SSL not working
- The performance of redis pipeline mode is unstable HOT 3
- CLIENT SETNAME?
- redis:get(key) question HOT 2
- opm package outdated HOT 1
- Connection pool not creating HOT 2
- How to create a single redis connection HOT 5
- this support two-way authentication?
- Add keepalive for redis client to make the connections reliable
- Nginx 1.24 HOT 2
- Nginx creates more connection than poolsize, backlog to redis HOT 5
- lua tcp socket read timed out, context: ssl_certificate_by_lua*, client: 172.69.58.204, server: 0.0.0.0:443 HOT 2
- redis set_keepalive doesn't work HOT 1
- lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/redis.lua:357: bad request HOT 1
- no request found HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lua-resty-redis.