0

I followed the Rails reference to stream some data to the client.

Is the Sleep necessary since I have seen the usage of Sleep everywhere? Does Rails internally use Sleep in Live to stream data?

1 Answers1

0

The sleep function in Rails is used to provide a delay of a specified length of time. In the case listed it looks like sleep 1 is providing a short pause (roughly one second) after each iteration in your loop, following

response.stream.write "hello world\n"

So it's not exactly necessary, but it will serve to space the output across a smoother time-frame, rather than having the whole lot output at once.

Peter David Carter
  • 2,548
  • 8
  • 25
  • 44
  • 1
    Thanks Peter, I have seen the streaming to fail sometimes when the `sleep` is not there. This is seen when we are streaming lots of data. The it failure is manifested for the process to hang and streaming stops. If I add the `sleep` with any value the streaming happens smoothly without any issues. – Vikram Anand May 18 '16 at 12:50