Files in /lib or /server


I am new to Meteor, so thanks in advance for the help!

I was looking through the Meteor documentation and sample applications, and am struggling a bit with the file/folder structure of my project.

I understand that all code placed in /server only loads/runs on the server - which is great for security reasons. I would have expected ALL DB calls to be only in the /server files. However, I have seen many javascript files with DB calls in /lib files as well.

Can someone please explain why/where is the best place to put the ‘backend’ DB accessing code?



I am a bit new to meteor myself, so someone more experienced please correct me if i am wrong.

Meteorjs allows you to do something called latency compensation (for example, simulation a db call on minimongo on the client) when you place a piece of code in the lib folder, so the code is executed on both the client and the server. This is typically done to provide the user with a smoother UX experience.

More specifically, rather than waiting for a call to execute the server and waiting for the result to return to the client, you can simulate the call on the client and immediately execute any following actions right away (so saving the round trip time to server and back).

Sasha has written a few posts on latency compensation, and I hope they’ll help: