Note that there are some explanatory texts on larger screens.

plurals
  1. PORedis long-polling Pub/Sub frequent message blocking
    text
    copied!<p>I'm trying to wrap my head around the Redis Pub/Sub API and setup a long-polling server.</p> <p>This lua script subscribes to a 'test' channel and returns new messages received:</p> <p>nginx.conf:</p> <pre><code>location /poll { lua_need_request_body on; default_type 'text/plain'; content_by_lua_file '/usr/local/nginx/html/poll.lua'; } </code></pre> <p>poll.lua:</p> <pre><code>local redis = require "redis"; local red = redis:new(); local cjson = require "cjson"; red:set_timeout(30000) -- 30 sec local resCon, err = red:connect("127.0.0.1", 6379) if not resCon then ngx.print("error") return end local resSub, err = red:subscribe('r:' .. ngx.var["arg_r"]:gsub('%W','')) if not resSub then ngx.print("error") return end if resSub == ngx.null then ngx.print("error") return end local resMsg, err = red:read_reply() if not resMsg then ngx.say("0") return end ngx.say(cjson.encode(resMsg)) </code></pre> <p>client.js:</p> <pre><code>var tmpR = 'test'; function poll() { $.get('/poll', {'r':tmpR}, function(data){ if (data !== "error") { console.log(data); window.setTimeout(function(){ poll(); },1000); } else { console.log('poll fail'); } }) } </code></pre> <p>Now, if I send <code>publish r:test hello</code> from redis-cli, I receive the message on the client and the server responds to redis-cli with <code>1</code>. But, if I send two messages quickly, the second message doesn't broadcast and the server responds with <code>0</code>.</p> <p>Are my channels only capable of receiving a message per second, or, is this a throttle on the frequency of messages a user can broadcast to a channel?</p> <p>Is this the right way to approach this polling server on nginx assuming many users may be connected at one time? Would it be more efficient to use GET requests on a timer?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload