0

I'm new to Server Sent Events... I have a recurring background rake task that, every minute runs a calculation. I would like the results of this calculation to be sent to my webpage where the JS will parse it accordingly to show on the page.

I'm not sure how to achieve this... the rake task is working, each minute, the calculation is being run. I've followed an SSE tutorial to correctly implement SSE. Now... I'm not sure how to combine the two...

SSE is run from inside a controller action, that corresponds with a page that events are published to. But then... calling a controller action in a rake task seems to defy MVC convention (though if I misunderstand and it doesn't, I can follow the answer here). So how else can I achieve this?

james
  • 3,989
  • 8
  • 47
  • 102
  • a rake task running some calculation and you want to make it available to your users through a controller ?. Store it somewhere, and query it from your controller. I think that's the best thing you can do here, at least with what you have. – fanta Feb 05 '20 at 22:21
  • but then... the controller has to "recurringly" be querying wherever this calculation is stored... which... isn't that the entire purpose of a recurring rake job? – james Feb 05 '20 at 22:22
  • Cron jobs are mean to run at every x time. Rake tasks I see them as something you manually run sometimes, but you could use them the way you're using them right now. Either cron jobs or rake tasks run async because they execute code that could block process, or their processing is too heavy, etc, etc. So, once your rake task/process/whatever finishes, you have the result, in the controller you'd only query for the result, you won't do any other calculation. – fanta Feb 05 '20 at 22:27
  • We can move to chat if you have time/prefer. I'm still confused though, when you say "in the controller you'd only query for the result", what I'm saying is, how do I tell the controller to only query every x time (i.e., when the rake task finishes)? Otherwise, it'll never query unless called, am I misunderstanding something? – james Feb 05 '20 at 22:30
  • Actually I think I did misunderstand... I read a little more carefully, it seems SSE in the first place is itself recurring... the controller action will continually send a stream of data unless the front end calls a `source.close()`. @fanta is that correct? if so then really I don't need a rake task at all and can just use the SSE – james Feb 05 '20 at 22:38
  • yes, it keeps the connection open until you close it. – fanta Feb 06 '20 at 16:16

0 Answers0