Search code examples
node.jspolling

How to poll another server periodically from a node.js server?


I have a node.js server A with mongodb for database. There is another remote server B (doesn't need to be node based) which exposes a HTTP/GET API '/status' and returns either 'FREE' or 'BUSY' as the response.

When a user hits a particular API endpoint in server A(say POST /test), I wish to start polling server B's status API every minute, until server B returns 'FREE' as the response. The user doesn't need to wait till the server B returns a 'FREE' response (polling B is a background job in server A). Once the server A gets a 'FREE' response from B, it shall send out an email to the user.

How can this be achieved in server A, keeping in mind that the number of concurrent users can go large ?


Solution

  • I suggest you use Agenda. https://www.npmjs.com/package/agenda With agenda you can create recurring schedules under which you can schedule anything pretty flexible.

    I suggest you use request module to make HTTP get/post requests. https://www.npmjs.com/package/request