Using imports, how does the server com with the client?

I’m confused by how we import [Publish] functions and [Meteor Methods] that reside under /imports/server to the Blaze client (either in /imports/client/ui or even just under app-name/client).

The classic way, is to just use the publish or meteor method, and everything is consumed by the client without imports. But if everything resides under the /imports directory (including the Blaze templates) how does that work? Are there real examples out there?

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');
     ...
    }
  });
});

How do server side only stuff make its way to the client in the new imports syle of developing?

At some point you need to import this at startup. Here is our structure :

/client/main.js : import '/imports/startup/client';
/server/main.js : import '/imports/startup/server';
/imports/startup/client/index.js : import /imports/client/ui/test.js
/imports/startup/server/index.js : import /imports/server/publishing/test.js

the client and server folder are special in meteor

Hope this helps,

1 Like

It sounds to me like I’m going to have to import on the server, the stuff in the imports/server directory, then publish it as we do in classical style. I just don’t know what that looks like.

// app-name/imports/server/trades-pubs.js

  // This code only runs on the server
  Meteor.publish('trades', function tradesPublication() {
    return Trades.find({},{sort: {timestamp: -1}, limit: 1000});
  });

// app-name/imports/server/trades-methods.js

Meteor.methods({
  // Only on server
  'trades.importFromFiles'() {
      fs = require('fs');
      const path = "/home/daemmon/trades_data/";      
      var files = fs.readdirSync(path);     
      ...
  }
});

// app-name/server/main.js

import '../imports/server/trades-methods.js';
import '../imports/server/trades-pubs.js';


Is this all that’s needed to get a publish methods to the client and server side meteor methods avaialbe to the client?

// 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('trades');
     ...
    }
  });
});