Streaming with Rack

Although we could improve our metrics application to show beautiful graphics and reports, we will instead provide a way to export the data stored in the database so it can be rendered in a third-party service or tool.

The default approach to send data from the server to the client in Rails is to use the send_data method. However, this approach requires us to generate upfront the whole string we want to send, which may take time and require a lot of memory as we store more metrics in our database. To solve this problem, we’ll stream data, allowing us to send data to the client in chunks without allocating a huge blob of memory.

In Chapter 5, Streaming Server Events to Clients Asynchronously, we used Rails’s live-streaming ...

Get Crafting Rails 4 Applications, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.