Persistent data on client only

hi!
i would like to persistently store data on the client side only.

session is destroyed once the app is closed.
database on the client seems to always be synced with the database on the server (i don’t want to store anything on the server …)
i am looking for something like a cookie.

what’s the meteor way of doing this?

thanks in advance.

There really isn’t any way to make anything persistent on the client, because they could always clear history/cookies/local storage.

But you could always use Meteor._localStorage if need be

thanks for your quick reply. it is perfect if the data is stored until the user clears the cache.
is _localstorage a package?
what is the syntax to read and write from local storage?
THANKS!!!

It is just a wrapper for standart browser’s window.localStorage

? thx.
still i don’t understand.

– do i have to install a package?

– how do i write and read a variable into local storage?

In simple words Meteor._localStorage = window.localStorage. This is a browser storage like a SessionStorage. You can use window.localStorage API, you do not need any package. A little sample:

Meteor.startup ->
  localCart = window.localStorage.getItem('localCart')
  if localCart
    UserCart.insert(localCart)
    # now we can watch a cart
    UserCart.observe
      changed: (doc) ->
         window.localStorage.setItem('localCart', doc)

We use alaSQL to maintain a persistent copy of the DB on the client. Works very well but you have to sync it.
Our app works connected and disconnected.

https://atmospherejs.com/agershun/alasql

1 Like

______________ thank you!!!

hmmmm. i can’t get it to work:

this is ok:
scoreWst = windows.localStorage.getItem(‘scoreWst’) || 0;

this throws an error:
scoreWst = Meteor._localStorage.getItem(‘scoreWst’) || 0;

–> Uncaught TypeError: Cannot read property ‘getItem’ of undefined

Defined when you add any accounts package. You can wrap this as

Meteor._localStorage = Meteor._localStorage || window.localStrage
1 Like