Persistent data on client only


#1

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.


#2

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


#3

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!!!


#4

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


#5

? thx.
still i don’t understand.

– do i have to install a package?

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


#6

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)

#7

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


#8

______________ thank you!!!


#9

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


#10

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

Meteor._localStorage = Meteor._localStorage || window.localStrage