I'm developing a chat module for my employer, and one of the requirements is that I "get enough backlog to fill the screen" when opening the room, instead of always limiting it to a specific number for each query (such as I'm doing right now)

I don't know if I should be dynamically changing the number to query on the client, or should I pass the client's window dimensions to the server, and have the server do some complicated math based on the that size, and what messages are currently available to query.

Is there any standard way of handling this? I looked at Facebook Messenger as a model, and in many cases, it seems to be returning 19-20 at a time, but I've seen some windows where there's 50+. It seems to depend on whether or not there's an image to display, which would fill up a good chunk of the screen.


It is sort of the standard pagination/line wrap problem in that you don't know how large the content is until you render it.

I don't think there is any magic solution, you just have to render and measure the content and the window size before you know if you need more.

Obviously you can guess and not care if it doesn't quite fill the screen, or ask for too much to fill even the largest screen with the smallest font.

| improve this answer | |

I'd do it in "pages", similar to paging through SQL results. Make a page a decent number of backlog entries (say 25 for the sake of this example.) Retrieve the height of your display area, create a div inside it for entries (I'm assuming you're using HTML for display as you tagged this with "javascript" - otherwise use whatever visual container is equivalent to a div in your GUI), request a page of backlog, insert them into the div, measure the height of the div once the entries are in, and if the div doesn't take up the display area yet keep requesting an additional page of backlog until it's full.

Make the number of entries per page a number that'll fill the average screen. This will also have the advantage that on slower connections people don't have to wait for the entire backlog, they'll get the most recent 25 rows first, and it'll fill the rest at worst a second or two (depending on their connection speed) later. Tweak the numbers to find a good balance between the size of data returned and eliminating waste.

If you really have to do it in one single request, assume one entry fills exactly one row of text (even though in chat it may end up as multi-row paragraphs), and request that many entries (and simply ignore the overspill).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.