Store client-side data, making it available to a service worker
Asked Answered
B

2

6

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.

Bangui answered 21/12, 2015 at 8:23 Comment(0)
O
6

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.

Oscular answered 21/12, 2015 at 15:58 Comment(0)
D
2

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.

Damek answered 31/12, 2015 at 17:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.