The simplest (but may be not the best) way is to use external shared
data services like Redis, as mentioned in the following thread:
https://groups.google.com/forum/#!topic/openresty-en/McUECt7YgPc
A much more efficient (though much more complex) way is to use a
shared data service supporting message multiplexing within a single
stream-typed connection and to use the upcoming ngx.thread.semaphore
API to coordinate requests' "light threads".
local server = require "resty.websocket.server" local redis = require "resty.redis" local function subscribe(ws) local sub = redis:new() sub:connect("127.0.0.1", 6379) sub:subscribe("chat.messages") while true do local bytes, err = sub:read_reply() if bytes then ws:send_text(bytes[3]) end end end local ws, err = server:new{ timeout = 30000, max_payload_len = 65535 } ngx.thread.spawn(subscribe, ws) local pub = redis:new() pub:connect("127.0.0.1", 6379) while true do local bytes, typ, err = ws:recv_frame() if ws.fatal then return elseif not bytes then ws:send_ping() elseif typ == "close" then break elseif typ == "text" then pub:publish("chat.messages", bytes) end end ws:send_close()and client-side:
<html>
<head>
<script>
var ws = null;
function connect() {
if (ws !== null) return log('already connected');
ws = new WebSocket('ws://127.0.0.1/test/');
ws.onopen = function () {
log('connected');
};
ws.onerror = function (error) {
log(error);
};
ws.onmessage = function (e) {
log('recv: ' + e.data);
};
ws.onclose = function () {
log('disconnected');
ws = null;
};
return false;
}
function disconnect() {
if (ws === null) return log('already disconnected');
ws.close();
return false;
}
function send() {
if (ws === null) return log('please connect first');
var text = document.getElementById('text').value;
document.getElementById('text').value = "";
log('send: ' + text);
ws.send(text);
return false;
}
function log(text) {
var li = document.createElement('li');
li.appendChild(document.createTextNode(text));
document.getElementById('log').appendChild(li);
return false;
}
</script>
</head>
<body>
<form onsubmit="return send();">
<button type="button" onclick="return connect();">
Connect
</button>
<button type="button" onclick="return disconnect();">
Disconnect
</button>
<input id="text" type="text">
<button type="submit">Send</button>
</form>
<ol id="log"></ol>
</body>
</html>
ws:send_text(bytes[3])
ws:send_text(bytes[1])
Hello,
Can you explain why using REDIS PUBSUB is less efficient than using nanomsg for example?
Hello!
The real goal here is to use a small number of backend connections for
On Sat, Aug 16, 2014 at 7:30 AM, Jerome Lafon wrote:
> Can you explain why using REDIS PUBSUB is less efficient than using nanomsg
> for example?
>
all the potentially very large number of incoming websocket or http
connections.
But I think we can also use redis pub/sub to emulate TCP-connection
level multiplexing by devising our own dispatch mechanism within Lua.
For example, we can have a background "light thread" doing the message
dispatch (to all the downstream requests within the current nginx
worker) from a single (or a very small number of) redis connections.
For synchronization between this background "light threads" and
request "light threads", we can use polling based on ngx.sleep() or
more efficiently, use the upcoming ngx.thread.semaphore API for it :)
Regards,
-agentzh
--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.