Managing many template-level subscriptions


#1

Hi all, I’m wondering what is the best way to handle a large number of template-level subscriptions using Blaze. So for example, I have an HTML table and each table row is an instance of a template with a subscription. Is that already a red flag? Generally this works when the table is paginated, but in a case where it doesn’t make sense to paginate, it would be a terrible experience to wait for all of the data to load for the entire table. So by having each row template subscribe to a single record, the entire table is built immediately and each row waits for its subscription to be ready before rendering its cells.

However, I understand the concept (from Kadira) of observer reuse, and it surely can’t be a good practice to have hundreds or thousands of different subscriptions each based on its own unique _id. And without coming up with a way to queue the subscriptions, they are all hitting the server at the same time, which probably compounds the CPU problem.

What is the appropriate implementation for a case like this?


#2

You don’t have to wait for a subscription to be ready (with this.subscriptionsReady()) . The data will arrive in chunks on the client so that you can already render these data.

So if you subscribe to Posts.find() and render a table-row for each post in this collection without waiting for the subscription to be ready, the rows will render one by one (or at least in chunks), rather than everything at once.


#3

Thanks @macrozone, I’ve been thinking about this. I’ve always assumed that it was necessary to show a loading indicator because I didn’t know about the data arriving in chunks.

Let’s say, however, that I am caching the subscription. So if the user navigates away from the route and then back to it, there could be hundreds or thousands of rows (or divs) to render. Is there a best practice for gradually rendering the dom elements so that the browser doesn’t freeze for a few seconds?