Split client and server - meteorjs

I am using latest version of Meteor.

As you know Meteor wants to keep everything inside same directory.

Like:

MeteorProject
   -- .meteor
   -- client
   -- imports
   -- server
   -- test
   -- node_modules
   -- package.json

I have create a public repo with a very simple reproduction scenario:

https://bitbucket.org/hassansardarbangash/meteor-react-boiler

What I need is that I want host server and client on separate server machines, since this is the most viable approach to have a good architecture.

Or may be I want to host admin panel client side on separate server, so here I have 3 separate services.

There is also a possibility that I want to create 3 more Reactjs based applications, and I would like to use same meteor server for all the transactions or api calls etc.

So is there any possibility how can I separate client and server in Meteorjs?

In general 1 Server build serves 1 Client bundle content. You want more and different clients, you engage multiple servers that generate multiple client bundles, servers that eventually connect to the same DB. Or you can write your client in something else and connect to a Meteor server via a REST API, GraphQL or something else.

When you refer to host, you generally imply the host of your server. The client’s host by default is the browser. If you mean to say - deliver the client not from server but from somewhere else … that would be a CDN delivery of your client bundle which is common with Meteor.

if i use meteor server using rest api, then what is the point of using meteor? And there is a very common case where I can have multiple clients on different servers or I can have native android and ios applications, seems like meteor doesnt support that via DDP

1 Like

Meteor supports just about anything with DDP. DDP.connect()

A Meteor app can have multiple concurrent DDP connections. If you separate your Admin and Consumer logic into two different Meteor apps, your Admin app can connect to its own server with default DDP connection. It can also open a connection to the Consumer app’s server at the same time.

Another option is to share a single database for your Admin & Consumer apps. To achieve this you need to work with your MONGO_URL environment variable.

Another option, as mentioned by @paulishca is to use a REST API from one to the other. Add an API to your Consumer app and have the Admin app hit it when necessary.

Hope that helps

3 Likes

How can I set this property? Any examples?

Using DDP.connect
This can be done on the server or the client. If you want to make calls to it directly from the browser put it on the client, if you prefer from the server for more security put it there, if you want to handle both, put it in code that is processed by both client/server.

Make sure the connection is global so that you can use it anywhere. Something like this I imagine:

const remoteApp = DDP.connect(url);
export { remoteApp };

Then call as you would normally with Meteor.

remoteApp.call("method_name",{ _id : "an_id"},function(err,result){
    if(err){
       // Handle the error
    }else{
        // Work with the result
    }
});

The docs for DDP.connect say that you can use:

  • subscribe
  • call
  • apply
  • methods
    etc.

They don’t mention callAsync, but you can try that as well and see what happens.

Sharing A Database
The quickest method with the least coding is to share a database. Locally you can run your two apps with the following process:

Sharing Locally

// Open two terminal tabs, one on Admin app directory, one on Consumer app directory
// Meteor sets its local mongodb instance on the next sequential port, so if app is running 
//on port 3000 its mongodb is running on port 3001

// Run the Admin app first
meteor run --port 3000 --settings settings.json

// Hop over to the Consumer app terminal tab
export MONGO_URL=mongodb://localhost:3001/meteor
meteor run --port 3002 --settings settings.json

If you do it this way remember that which ever app ‘owns’ the database, if you shut it down the database will shut down, so the second app will start throwing Mongo connection errors.

Sharing on Galaxy

//Deploy Admin app with free mongo instance
DEPLOY_HOSTNAME=eu-west-1.galaxy.meteor.com meteor deploy [appname]  --settings settings.json --owner [organisation-name] --mongo
// You will get back a MONGO_URL in the CLI that looks something like this
//mongodb://****:**********.servers.mongodirector.com:27017,SG-eugalaxycluster-38732.servers.mongodirector.com:27017/*****?replicaSet=RS-eugalaxycluster-0&ssl=true.

// Open your settings.json file for your Consumer app and add the following
{ 
    "galaxy.meteor.com": { 
        env: { 
            MONGO_URL: "The Mongo URL you got back from Admin app deploy"
        } 
    }
} 

// Hop over to the Consumer app terminal tab
// Deploy the consumer app
// Don't include a free mongo instance with this deploy
DEPLOY_HOSTNAME=eu-west-1.galaxy.meteor.com meteor deploy [appname]  --settings settings.json --owner [organisation-name] 

Using a REST API
Although it’s a very old package I like to use simple:json-routes, because it’s simple :slight_smile:

Hope that helps

3 Likes