In my node.js chat app, I want to save chat data in offline mode and push notification when online. Basic chat app is done. I'm using ws node module for server code. In client side code, I want to use service worker for storing offline data. I have done some home work https://jakearchibald.com/2014/offline-cookbook/ https://ole.michelsen.dk/blog/making-an-offline-webapp-with-service-workers.html, but I think I don't understand how can I store chat data in service worker offline data.
IndexedDB is a general purpose database that's available client-side in both the service worker and main web page context. I'd suggest using that to store data like chat messages.
There are a number of libraries out there that wrap IndexedDB to make the interface friendlier. A relatively newer one, idb
, has the advantage of providing a promise-based interface, which comes in handy when using IndexedDB from a service worker, since so much of the asynchronous code there is already promise-based.
Once I also have to faced that type of problem. So what I do, is some kind of cheating. I create an object in IndexedDB where I store every chat data temporally and after successfully send my chat data to server, I immediately delete my IndexedDB data. If chat data failed to send to server, it cannot delete temporally IndexedDB data. And when server is available to receive chat data, first check IndexedDB data and send to saved chat data (if any left).
NB: this is not accurate solution, not even close.
© 2022 - 2024 — McMap. All rights reserved.