Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Implement caching and batch updates for bus location data #61

Description

@IdkwhatImD0ing

Issue description:

Currently, the bus tracking system's server built with Express.js is directly updating the Firebase server for every incoming POST request with bus location data. This causes the Firebase server to be updated too frequently, especially when there are many buses sending data simultaneously. To optimize the system and minimize the likelihood of reaching Firebase's rate limits, we need to implement a caching mechanism and batch updates for the bus location data.

Proposed solution:

  1. Implement an in-memory cache using a JavaScript object to store incoming bus location data.
  2. For every incoming POST request, save the bus location data to the cache.
  3. Periodically update the Firebase server with the cached bus location data (e.g., every 5 seconds).

Sample implementation:

const busLocations = {};

app.post('/bus_location', (req, res) => {
  const { busId, location } = req.body;

  busLocations[busId] = location;

  res.status(200).json({ message: 'Location data received' });
});

setInterval(() => {
  Object.entries(busLocations).forEach(([busId, location]) => {
    const firebaseRef = admin.database().ref(`YOUR_FIREBASE_PATH/${busId}`);
    firebaseRef.set(location).catch((error) => {
      console.error(`Error updating bus ${busId} location: `, error);
    });
  });
}, 5000);

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions