Client Or Server Or Both (newbie question)

I know similar topics has been covered many times in the forum and other places, but i can’t seem to find the exact answer i’m looking for.

Should my inserts into my db be on the server side, client or both? I have /client, /server directory structure. Seems weird to have them on client side when an insert could be entered into the browser console.

I am so used to php backend development that the process is a little odd for me to get my head around. Seem like all my database work would be on the server side from what I am used to, but i want the app to be able to work offline if there is a network interruption.

Should I be utilizing publishing & subscriptions for this?

Thanks in advance.

It’s pretty confusing for a new developer, but it’ll get better I promise.

You want db writes on the client sometimes becase Meteor has what’s called Optimistic UI. This means, if you save something it’ll reflect in the UI even before it saves to the database. It makes things feel snappier.

Let’s say you have a Meteor method declared in /lib/methods/post.js:

Meteor.methods({
  'Post.create': function(post) {
    Posts.insert(post);
  }
});

Since that method is in a place that both the client and server can access (the lib folder), you get that benefit of optimistic ui.

Where possible your db interactions should be done in a place both server and client can access.

When you get a better understanding you’ll see that sometimes you shouldn’t expose certain methods to the client. But that comes later, for now just grok this.

Thanks for the response. Definitely helps clarify things. I have a directory as such /collections/players/create.js

From what i have read this directory structure should work on both client and server. I also have a /lib directory for things that i want to run first. Should I be using the /lib directory as you specified instead of /collections or is it personal preference?

/client => Run on client.
/server => Run on server.

Everything else runs on both. :rocket: You can name your folder cesarsalad and it’ll run on both client and server.

1 Like

Yes, lib is just for making some things run first before your other code. It’s up to you whether you want to put collections in there.

Thanks guys. So I have this in /collections/players/create.js

`players = new Mongo.Collection("players");

Meteor.methods({
'submit form': function(event){
    //prevent default form submit
    event.preventDefault();
    //set var to retrieve input field from form
    var firstNameVar = event.target.firstName.value;
    var lastNameVar = event.target.lastName.value;

    //insert into players collection
    players.insert({
        firstName: firstNameVar,
        lastName: lastNameVar,
        createdAt: new Date(),

    });//player insert
}//end submit form
});`

How do i link it to the client so the form submits to the method? Everything worked fine when all my js was in one file, now that i’m splitting them up i’m at a loss. I’m not using any schema at this point.

Should i use Meteor.call in the /client/player/create.js like this?

`Meteor.call('submit form', {
//what goes in here?
},`

Lastly would “Meteor.call” go in the Template.Main.events like this or somewhere else like after it?

`Template.addPlayerForm.events({
someName: function () {

    
}

});

I’m probably just over complicating it, but my mind is mush trying to separate the code into different directories. Thank you for any help in advance.

This might be a bit more complicated than you’re looking for, but here is what I would do in a production app: http://guide.meteor.com/methods.html#method-form

There are four exceptions to this:

  • packages - local packages go here
  • tests - test files go here
  • private - private application assets go here
  • public - static assets go here

read more