Using cookies in meteor

Hi I am looking for a cookie type implementation using meteor. Can you pls let me know how to go about it? Or is there any other way similar to cookie implementation in meteor?

What are you trying to do?

If you want to store something on the client, you can just use local storage.

http://dev.w3.org/html5/webstorage/
http://www.html5rocks.com/en/features/storage

what is the best or what is the different between storing in a cookie as opposed to local storage?

all I am trying to do is store a user identification token. So not sure if cookie is the right storage or local storage or session storage.

For that you’d usually use a cookie. For packages that make it really easy to use, look here: https://atmospherejs.com/?q=cookie – the first 4 results all look solid.

1 Like

Meteor’s Session is lost whenever the user reloads the page, so it’s not persistent at all and mostly only useful for storing state relative to the current UI, which would be lost on page reload anyway.
And HTML5 local storage is a storage option to be aware of and read a bit about. It’s like a client-side persistent cache or session. It’s useful, mostly the use case is “I need to store lots of data purely client-side” which rules out cookies (they have a hard size limit) and makes storing on the server unattractive (beause it just causes unnecessary load).

1 Like

I put the jquery-cookie script somewhere in the client folder and then stuff like this works just fine:

$.cookie("post_id", post_id, {expires: 365, path: '/'});
$.removeCookie("post_id", {path: '/'});
var cookieValue = $.cookie("post_id"); 

I see some folks have packaged that script up and made it available on atmosphere (https://atmospherejs.com/?q=jquery-cookie). Much better approach than just throwing the script somewhere in the client folder.

2 Likes

I am using the chuangbo cookie but if the do this just to test, I get an error
ReferenceError: Cookie is not defined

Cookie.set(‘tokenId’, ‘1f8fad5b-d9cb-469f-a165-70867728950e’, {
domain: ‘rakshak.meteor.com’,
path: ‘/’,
expires: 30
});

I am doing this in server.js code.

Cookie is used on the client side only

1 Like