Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 818
user2013 Avatar asked Dec 21 '15 08:12

user2013


People also ask

Can service workers access LocalStorage?

LocalStorage is not accessible from web workers or service workers. Cookies have their uses, but should not be used for storage.

Can service workers access cache?

Using a Service worker you can easily set an app up to use cached assets first, thus providing a default experience even when offline, before then getting more data from the network (commonly known as Offline First).


1 Answers

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.

like image 190
Jeff Posnick Avatar answered Nov 24 '22 19:11

Jeff Posnick