lundi 27 décembre 2021

What is the best practice to update a list view in real-time for web application?

Scenario

Imagine a transporatation company a has large number of trucks. Each truck has its metadata stored in an indexed summary ready for query, such as plate/location/speed/ETA/status/driver etc. Operators can use any filter/sort to get a paged list result in a web app, which fetches data from a RESTful API GET: {baseUrl}/trucks backed by a SQL server.

As the data is rapidly changing in nature(truck moves every sec!), we can imagine that the list result can possibly change anytime when any truck's metadata changed. And further more, this change can be filter/sort specific: e.g. a drive swap for a truck may only affect listing result for operators who filter/sort by driver, or the listing result actually contains that truck.

Target

Let this "list view" auto updates whenever the list result changes.

Current apporach

Web app does polling every minute (So call GET: /trucks every minute) for a fresh listing result (So short polling, yes). However, this approach is not smart enough as in some cases data changes a lot faster than a minute, while in other cases a lot of traffic are wasted for no change at all. Also it's not recommended in this answer, which I fully agree with.

Problem

I was thinking of long polling or websocket to let server pushes update to the web app whenever the list result changes. However, it's easy to track changes for a single truck but not for a list, as a list could change when any truck gets updated in the database.

For example, if we have trucks with the following data:

| Plate | Driver Name | Status | | -------- | ----------- | ------ | | PLATE-01 | Aaron | Running | | PLATE-02 | Ben | Running | | PLATE-03 | Charlie | Running | | PLATE-04 | Daniel | Running | | PLATE-05 | Edison | Running | When a user lists trucks by driver's name for page 2 with size of 2, he gets truck 03/04. then his list should get updated when:

  1. Truck 03/04 has an update, for example truck 03 stopped, list result remains the same trucks but different driver values.
  2. Truck 02 has driver swapped to Francis, then list result should become truck 04/05.
  3. Truck 05 has driver swapped to Adam, then list result should become truck 02/03.

It's easy to capture/track the change for scenario 1, we can simply track every truck in the list result. But I cannot see any good way to solve scenario 2/3, apart from tracking every truck in the database, which is not possible.

Question

What is the best practice/design to update this list view in real-time?

Aucun commentaire:

Enregistrer un commentaire