lua nginx websocket
Nginx like a lot of projects can be extended with Lua scripts very easily.
lua-resty-websocket library is part of openresty project and started to package it for Debian.
In this short post, I will present how to have a simple WebSocket server that pulls data from Redis.
You will need nginx-extras lua-nginx-websocket lua-nginx-redis redis-server
packages from Debian.
You can have an Nginx config like that for example:
server {
.test;
websocket$app_root /u/websocket-test;
location ~ /s {
$app_root/websocket.lua;
}
}
And the websocket.lua
file:
local server = require
local redis = require
-- websocket server
local wb, err = server:
if not wb
-- redis connection
local rd = redis:
local ok, err = rd:
if not ok
After the init, we will get data from redis and send it to the websocket client.
while true
wb:
rd:
Now on the JavaScript client.
;
socket.onmessage = ;
For testing, you can use redis-cli monitor
and another command to insert
data on the Redis list.
Edit: Experiment project znc-urltimeline