I'm new to openresty, nginx and lua in general.
I'm trying to have subscription replies from redis as chunked-transfers out from nginx. My code is below based on the simple examples given in the documentation.
You connect to it via http://localhost:8081/SUBSCRIBE/subscribechannel and it send back json encoded replies.
I'm guessing the neverending loop is a little brutal but it seems to work. What I am seeing on the client side from browsers is a buffering of about 1k (all on a local machine) before chunking seems to start happening - regardless of flushes etc. I get a ks worth and then chunks seem to come as expected. My client side code is proven and it behaves similarly in different browsers - so I'm guessing it is server side.
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8081;
location /SUBSCRIBE {
chunked_transfer_encoding on;
content_by_lua '
local redis = require "resty.redis"
local redis_subscribe_path = string.match(ngx.var.uri, "[^/]+$")
local cjson = require "cjson"
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(86400000) -- 1 day
red:set_keepalive(0, 1024) -- 1 day
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("1: failed to connect: ", err)
return
end
local res, err = red:subscribe(redis_subscribe_path)
if not res then
ngx.say("1: failed to subscribe: ", err)
return
end
ngx.say("{\\"SUBSCRIBE\\":", cjson.encode(res), "}")
ngx.flush(true)
while 1 do
res, err = red:read_reply()
if not res then return
end
ngx.say("{\\"SUBSCRIBE\\":" , cjson.encode(res) , "}")
ngx.flush(true)
end
red:close()
';
}
}
}