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:
- Implement an in-memory cache using a JavaScript object to store incoming bus location data.
- For every incoming POST request, save the bus location data to the cache.
- 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);
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:
Sample implementation: