How to alert running REST API when a separate function is called?

I am using Nickel to run a REST API and I am wanting to use this approach to have a REST API that sends back "102 Progress" HTTP Status codes until it is actually ready, then sends back a 200.

The problem is that on my side, I am waiting for a user to perform some action. Once the thread knows that action was performed, our server is alerted and we can then respond to this REST request immediately with a 200 status code and go from there.

Which is great, but it then means that from the REST call, we have to be polling our own database in order to determine when this action occurred. And to some extent the whole point of this approach was to remove polling. It occurs to me that maybe with some fancy callbacks or events or someway the database knows which REST API thread to alert could know, we could respond to this user as soon as the function gets called when the user logins occurs?

Is there a way to remove that polling and instead when our separate function is called that includes the information we need to reply to the client, we automatically know from the REST request and can reply instantly?

If we can't remove this polling we'll probably just switch over to a system that calls a php script on our clients server to alert them when the user has performed the necessary action. Then let them do the callback or find a way around this.

Thank you!

Create a std::sync::mpsc::Channel, store it somewhere, then use the channel to wait the read handler and signal it from outside? Not sure if Nickel provides the needed hooks for "store it somewhere", but it's a possible approach.

This will of course use up a thread and a socket (AFAIK threads and sockets have about the same overhead under Linux). If you're expecting many thousands of simultaneous waits, a different solution would be needed.

1 Like