What is the differences between Session and Local (client-side only) Collection?
Asked Answered
P

3

8

In Meteor, I have a little confusion between Session and Local Collection.

I know that Session is a temporary reactive key-value store, client-side only, and is cleaned on page refresh. Local collection seems to be the same: reactive, temporary client-side storage, cleaned on page refresh with more flexible function like insert, update & remove query like server-side Mongo collection.

So I guess I could manage everything in Local Collection without Session, or, everything in Session without Local Collection.

But what is the best and efficient way to use Session and/or Local collection? Simply, when to use Session and not use it? And when to use Local collection and when not use it?

Parotid answered 10/12, 2014 at 15:25 Comment(0)
T
6

As I read your question I told myself that this is a very easy question, but then I was scratching my head. I tried to figure out an example that you can just accomplish with session or collections. But I didn't found any use-case. So let's rollup things from begin. Basically you already answered the question on your own, because it is the little sugar that makes collections something special.

When to use a collection?

Basically a collection is a database artifact. Imagine you have a client-server-application. All the data is persisted in the server side storage. Now you can use a local collection to provide the user a small subset of the servers collection. So a client collection is a database with reduced amount of data. The advantage is that you can access the collection with queries. You can use the same queries on server and client. In additon a collection always contains multiple objects of the same type. Sometimes you produce data on client for the client. No server interaction needed. Than you can use a local collection. A local collection provides the same functionality as a normal collection without server communication. This should be used if you have multiple objects with the same structure and in special if you'd like to use query operators.

You can also save the data inside a session object. Session objects can contain multiple objects as well. But imaging you want to find an object in an objectarray indexed with a special id. Than you need to iterate throw the whole array in order to find this object. You have to write additional logic, that can be handled with collection like magic. Further, collections return cursors. A cursor is an reactive object that just changes if the selected data changes. That means if you use find with an id. Than this object just rerenders when the object to this id changes. With session you can't. When a session changes you need to rerender all depending objects.

When to use a session?

For everything else. Sessions are often just small objects that contain some configuration logic. It is basically just one object and not a multiple occurency of equal objects. Haven't time now to go in detail but if it does not fit the collection use-cases you can use sessions.

Have a look at this post that describes why sessions should not be overused.

Tungusic answered 10/12, 2014 at 18:17 Comment(1)
Meteor prevents you from inserting documents client side unless you have the ID, which you won't have.Giovannagiovanni
D
4

I assume that by local collection you mean: new Mongo.Collection(null)

The difference is that local collections do not survive hot code pushes. A refresh will erase Session, but hot code push will not, there's special code in Meteor to persist the values of the Session variable in the case of a hot code push..

Dollie answered 10/12, 2014 at 22:22 Comment(0)
C
0

You would use Session whenever you're storing temporary values that do NOT need to be persisted to the database.

Trivial examples could include a users selection of filters or the item in an index vies that is currently selected.

manipulated data in minimongo (insert, update, delete etc) is intended to be sent back to the server and stored in the database. For example this could be updating a users profile information etc.

Cladding answered 10/12, 2014 at 15:44 Comment(3)
Great, let me know if you'd like a longer explanation, before marking it correct. :)Cladding
I understand now more the purpose of the Session and what it is used for. In Discover Meteor tutorials, he uses Local Collection to set an Error Collection that is not intended to be sent back to the server, that's why I was confused and didn't understand its real purpose. So I guess that when I want to send back the data to the server I have to use methods (insert/update) and custom methods, am I right?Parotid
yeah I think you got it.Cladding

© 2022 - 2024 — McMap. All rights reserved.