2

I have cron jobs launch when server start that watch and start an appointment when it's time come but I want to send data to my client through response ,is that possible in cron jobs

I am working with NodeJS and sails

crons.schedule("* * * * * *", async (req, res) => {
  res.send("appointment started")
}
mohamed nageh
  • 593
  • 4
  • 9
  • 26

2 Answers2

1

No, you can't do this, cause you don't have access to req & res objects

Yuriy Vorobyov
  • 755
  • 4
  • 8
1

As mentioned by @Yuri Vorobev, CRON jobs in Sails do not have access to req and res objects. But what you can do is call a controller method/Action in the CRON job and send response at regular intervals.

Prazhus
  • 683
  • 5
  • 6
  • I have called a controller in the cron jobs but it didn't send a response too , so let say I have this controller sendNotificatin (req,res ) how to call this in my cron job and send a response ? – mohamed nageh Dec 29 '20 at 07:11
  • Try `var sendNotificationController = rquire("./sendNotificationController.js");`. This way you can access all the controller methods. Eg: if there is a function called doSomething, then you can call it by `sendController.doSomething()`. – Prazhus Dec 29 '20 at 08:23
  • okay and the res how it will work on that case it will give no error @Prazhus – mohamed nageh Dec 29 '20 at 08:48
  • Do you have any strategy for sending response to the client without receiving a request from them ? But I feel it would be easier for you to use sockets. – Prazhus Dec 29 '20 at 12:18
  • I can do that yes without receiving a request but the issue how I will responded , yes socket.io is gonna work but I am looking for another way to do that – mohamed nageh Dec 29 '20 at 18:26