lua-resty-websocket icon indicating copy to clipboard operation
lua-resty-websocket copied to clipboard

use websocket client to sync data in local redis before nginx is accessible

Open x-077 opened this issue 3 years ago • 5 comments

Hello,

Im not sure if that's possible but before I look at openresty / LUA scripting, within my microservice (nodejs), I had a websocket client that was connecting to a remote server to sync up data into redis before the service was accessible.

Those data are relative to the authentication of the users, routes, certificates and should be available before the service is up.

Can I do the same thing directly on nginx level using LUA ( and lua-resty-weboscket / lua-resty-redis ) ? As websocket and redis are not usuable in the init_* block, Im wondering how I could proceed.

Thanks

x-077 avatar Jul 19 '21 22:07 x-077

sync data in the priviledge process, drop the request before the sync is completed.

zhuizhuhaomeng avatar Jul 23 '21 14:07 zhuizhuhaomeng

Hello @zhuizhuhaomeng ,

Im not sure to understand. openresty is quite new for me could you please be a bit more specific ?

Thanks

x-077 avatar Jul 23 '21 17:07 x-077

@matth-c3

Can I do the same thing directly on nginx level using LUA Could you please clarify the "same thing"? Do you want to use openresty to build a WebSocket server?

doujiang24 avatar Jul 25 '21 11:07 doujiang24

Hello @doujiang24 ,

No, I would like to use the lua-resty-websocket client to establish a persistant connection to a remote server, before openresty/nginx is resdy to accept connections. I would do that in the init_* block but its not possible with lua-resty-{weboscket / redis }.

x-077 avatar Jul 26 '21 08:07 x-077

okay, you can create a zero delay timer in init_worker_by_lua, like

init_worker_by_lua_block {
  local ok, err = ngx.timer.at(0, websocket_client_func)
}

you can do anything in the websocket_client_func then.

but just be noticed that the timer runs asynchronous, it won't stop openresty to accept connections.

doujiang24 avatar Jul 26 '21 14:07 doujiang24