Any directory named client/ is not loaded on the server. Similar to wrapping your code in if (Meteor.isClient) { ... }.
Meteor gathers all your JavaScript files, excluding anything under the client, public, and private subdirectories, and loads them into a Node.js server instance. In Meteor, your server code runs in a single thread per request, not in the asynchronous callback style typical of Node.
…Meteor ensures that any file in any directory named server/ will only be available on the server, and likewise for files in any directory named client/. This also precludes trying to import a file to be used on the server from any directory named client/even if it is nested in an imports/ directory and vice versa for importing client files from server/.
That particular question shows a publication (two actually). Meteor.publish can only be used on the server.
So, even if you imported that code to the client (and given the earlier discussion, above, you wouldn’t be allowed to), it wouldn’t work - there is no Meteor.publish on the client.
I wonder if you’re confusing import with “consume”, especially since it is possible to have Meteor.methods on the client?
In a nutshell, the client can consume server functionality - it can subscribe to a publication or call a method, but it may not always make sense (or be possible) to import server functionality. And as we’ve seen, the build tool does not permit code in a client/ folder to import code from a server/ folder - even if either or both are in an imports/ folder.
I reviewed that guild, and reviewed the “todos” code, I couldn’t find an example where the client import meteor methods or publish functions. Can someone help me or point me to code where I can help myself find examples please?
In the old days, I “consumed” meteor methods or publish functions without any effort. Today, I assumed (I know, that’s bad) we need to import everything – so that lead me to look for examples along these lines.
Also, I know I don’t HAVE to import anything, as Meteor still provides the “magic” as before. Yet, I’m looking to sort out my code to comply with the “new” way of JS and the industry, hence, I’m looking to import everything.
Are we saying here that we should NOT import meteor methods or publish functions, setting them outside the “new normal” way of things?
Ah - okay. So import (in Javascript) together with imports/ (the folder) allows you to control which files you want to include in your build - and where you want them to go (client, server or both). In addition, they also let you control load order, which may be important.
Back in the pre-import days, Meteor loaded everything it could see according to the conventions of folder names (client/, server/ and lib/ being the standard trifecta). If you needed to control load order, you had to follow the practise of alphabetising file names and/or building arbitrarily obscure folder trees.
So now you still have the eager loading described above, but you also have the ability to “only load what I tell you to load, where I tell you to load it”. Anything under an imports/ folder will not appear in the final app unless you import it. There is still client-only code, server-only code and code which can be on both. A running Meteor application is no different. Using import with imports/ just gives you complete control of how modules are stitched together for build.
Don’t know if that helps - I’m sure there’s a better explanation somewhere out there.
I read the docs and examples and understand what you’re written.
So, a simple question, how does a [Publish] function in [/imports/server/publishing] end up inside a Blaze OnCreated function where I can [Subscribe] to it [/imports/client/ui] in Meteor 1.5 if both are under the import directory?
Illustration:
// imports/server/publishing/test.js
Meteor.publish('publish.test', function() {
if (this.userId) return TestCollection.find({});
return self.ready();
});
// imports/client/ui/test.js
import { Template } from "meteor/templating";
import { ReactiveDict } from "meteor/reactive-dict";
import { Mongo } from 'meteor/mongo';
import TestCollection from '../../imports/collections/test.js';
import "./test.html";
Template.Test.onCreated(function() {
this.state = new ReactiveDict();
this.autorun(() => {
const subscription = this.subscribe('publish.test');
...
}
});
});
However, imports/server/ui/test.js should be imports/client/ui/test.js: these are for the client, so should not be in a server/ folder. Then in client/main.js something like:
Wait, so you’re saying from the traditional Meteor directories OUTSIDE the /imports directory, e.g., app-name/server and app-name/client, I should import from the /imports directory THEIR, and then use them as we did in prior times?
I tried to make an Illustration, but I just don’t understand. Is there an example somewhere?