Meteor multi-tenancy patterns and package

Meteor

Packages that can be used to create multi-tenant apps.

https://github.com/flipace/meteor-tenantify

https://github.com/mizzao/meteor-partitioner

This is a ripped down version of what’s in my main.js file in the client:

if (!Session.get('tenantId')) {
  var hostnameArray = document.location.hostname.split('.'), subdomain;
  if (hostnameArray[1] === 'mydomainname' && hostnameArray[2] === 'com') {
    subdomain = hostnameArray[0];  
  }
  if (subdomain) {
    Meteor.call('findTenantBySubdomain', subdomain, function(err, res) {
      var tenantId = res;
      if (tenantId) {
        Session.set('tenantId', tenantId); 
      }
    });
  }
}

It’s not pretty, but it works. Note: subdomains are unique to tenants, so we can get the tenant _id value by finding the right document by subdomain:

Meteor.methods({
  "findTenantBySubdomain" : function (subdomain) {
    check(subdomain, String);
    var tenant = Tenants.findOne({subdomain: subdomain});
    if (tenant) {
      return tenant._id;    
    }
  }
});

source: https://forums.meteor.com/t/multitenancy-and-meteor/5653/14