openresty / lua-resty-websocket

WebSocket support for the ngx_lua module (and OpenResty)
508 stars 106 forks source link

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

Open x-077 opened 3 years ago

x-077 commented 3 years ago

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

zhuizhuhaomeng commented 3 years ago

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

x-077 commented 3 years ago

Hello @zhuizhuhaomeng ,

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

Thanks

doujiang24 commented 3 years ago

@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?

x-077 commented 3 years ago

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 }.

doujiang24 commented 3 years ago

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.