Hello,
- I have an upstream server which publishes events via websocket and/or EventSource channels as necessary.
- I want my OpenResty app to know about and act on those events straightaway.
- I don't want the upstream server to have to maintain a list of downstream servers, so I want the downstream app to subscribe to the channel.
It'd be pretty straightforward to create a supervised process, running as a separate script on the OpenResty app servers, which listens to the upstream channel and informs the OpenResty app by hitting appropriate local URLs, according to incoming message data. (It's a pretty simple data model in this part of the application, and this would be consistent enough for our purposes.) But it seems like it'd be so much better/neater to do it inside OpenResty, if it can be done, without having to run supervisors, other script runtimes and so forth.
So is it possible to:
- open a long-running resty.websocket.client, lua-resty-http or other client connection when the OpenResty server starts;
- set a Lua event handler function to deal with incoming messages received on the connection,
- notify local URLs via HTTP requests in the handler function, and
- leave it running as long as nginx stays running?
- Ideally with a handler for socket errors so that it could try to reconnect if the connection failed?
If so, what's the best way to do it? And at what point in the system should this happen? I've been reading a lot about the various phases and when which APIs are available, e.g. not being able to call ngx.location.capture from "light threads" started by ngx.timer.at - but I'm struggling to get it at all clear in my head at the moment.
Would really appreciate some tips as to how to go about this, if it's possible!
Thanks very much,
Igor