Hi!
Thank you for the clarifications! :)
I did look before but couldn't get the examples to work. What I mean by that is "echo" functionality works, Redis pubsub does too, because I can see the messages in redis-cli in real-time, whereas they don't show on the chat example.
Is there maybe a listen function/command I should be interfacing to?
I'm referring to this code (server-side):
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>
Apart from the lack of error handling (which would be fixed later), are you able to pinpoint any of the issue at a glance?
Alternatively, do you think you could easily port Socket.io on top of Openresty?
Thank you Yichun, keep up with your great work! I'll sponsor your work ASAP :)
On Wednesday, August 13, 2014 11:50:41 PM UTC+2, agentzh wrote:
Hello!
On Wed, Aug 13, 2014 at 1:12 PM, Osman Tekin wrote:
> I'm trying to build a simple chat system with lua-resty-websocket, but I
> can't get it to work and especially I don't get what I have to do server
> side, I'm really lost.
>
Seems like I need to create a demo chat app atop openresty. It's much
easier for me than answering such general questions over and over
again on the list :)
> So far there are: send_text, send_binary, send_ping, send_pong, send_close,
> send_frame, recv_frame.
>
> I think recv_frame is used to receive server side sent events no?
>
Lua-resty-websocket library does have an official document:
https://github.com/openresty/lua-resty-websocket#readme
Please read it before asking questions :)
recv_frame is for reading websocket messages (or "frame" in the
websocket terminology) from the remote end.
> What I need is simply for multiple users to be able to send and at the same
> time to receive messages sent by other users.
>
> What would be the best way to accomplish this?
>
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".
Regards,
-agentzh