Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to synced data between same collection in different cluster (main app and search app)? #15

Open
radiegtya opened this issue Feb 17, 2015 · 32 comments

Comments

@radiegtya
Copy link

Btw How if I am removing my packages data from my main app collections? With this tutorial the data will be not synced between "packages collection" in main app and search app. I think it will be an issue. How to get it work (I mean the data between all collections are remain same/synced)? Thanks

@arunoda
Copy link
Member

arunoda commented Feb 17, 2015

There are couple of ways, you can do it.

Easy way

Use the same database for the both apps

Other way

Let's say you wan't connect to the web service's data. First create a publication on the web app.

// web app
Meteor.publish('myPosts', function(category) {
  return Posts.find({category: category});
});

Now, from the search service do this.

// in search app

var webAppConn = Cluster.discoverConnection('web');

webAppConn.subscribe('myPosts', 'my-category');
var Posts = new Mongo.Collection('posts', webAppConn);

// access data
console.log(Posts.find().fetch())

@radiegtya
Copy link
Author

Hi @arunoda
COOL! Well explained in very easy way. I'll try asap and report the result to You. I'll close this for now.

Thanks

@radiegtya
Copy link
Author

Sorry another question, from your example, how if I am inserting data from search app? How to sync them? Or just using manual webservice insert call, and manual inserting to search app collections? or are there any good solution?

Thanks

@arunoda
Copy link
Member

arunoda commented Feb 17, 2015

If you are using the same database, then you can simply do an insert as
normally. If not, you need to create a method on the main app(web
service) and call it from the search service.

On Tue Feb 17 2015 at 2:24:53 PM Ega Wachid Radiegtya <
[email protected]> wrote:

Sorry another question, from your example, how if I am inserting data from
search app? How to sync them? Or just using manual webservice insert call,
and manual inserting to search app collections? or are there any good
solution?

Thanks


Reply to this email directly or view it on GitHub
#15 (comment).

@radiegtya
Copy link
Author

Hi @arunoda

I think the answer should be manual call webservice insert. But after insert the data will be synced because of this (var Posts = new Mongo.Collection('posts', webAppConn);) right?

Thanks for the answer

@arunoda
Copy link
Member

arunoda commented Feb 17, 2015

Yes. for sure.

On Tue Feb 17 2015 at 2:46:08 PM Ega Wachid Radiegtya <
[email protected]> wrote:

Hi @arunoda https://github.com/arunoda

I think the answer should be manual call webservice insert. But after
insert the data will be synced because of this (var Posts = new
Mongo.Collection('posts', webAppConn);) right?

Thanks for the answer


Reply to this email directly or view it on GitHub
#15 (comment).

@radiegtya
Copy link
Author

Hi @arunoda, I have tried Your solution with simple code. I made two app, online and offline. I want my offline and online app are stand alone. But when I am using cluster, my app whether offline and online are having same display (which is offline display). Here are my codes:

/*=============== OFFLINE APP (localhost:3000) ===============*/
Posts = new Mongo.Collection('posts');

if (Meteor.isClient) {
    Meteor.subscribe('posts');

    Template.hello.helpers({
        posts: function() {
            return Posts.find();
        }
    });

    Template.hello.events({
        'click button': function() {
            Meteor.call('Posts.insert', {name: Math.random().toString(36).substring(7)})
        }
    });
}

if (Meteor.isServer) {
Cluster.connect("mongodb://localhost:27017/discovery");
Cluster.register("web");
    Meteor.publish('posts', function() {
        return Posts.find({});
    });

    Meteor.methods({
        "Posts.insert": function(doc) {
            Posts.insert(doc);
        }
    });
}
 /*=============== ONLINE APP (localhost:4000) ===============*/
offlineConn = Cluster.discoverConnection('web');    
Posts = new Mongo.Collection('posts', offlineConn);
//Ground.Collection(Posts);

if (Meteor.isClient) {
    offlineConn.subscribe('posts');

    Template.hello.helpers({
        posts: function() {
            return Posts.find();
        }
    });

    Template.hello.events({
        'click button': function() {
            offlineConn.call('Posts.insert', {name: Math.random().toString(36).substring(7)});
//            Posts.insert({name: Math.random().toString(36).substring(7)}); 
        }
    });
}

if (Meteor.isServer) {
Cluster.connect("mongodb://localhost:27017/discovery");
Cluster.register("online");


// access data
    console.log(Posts.find().fetch())
}

Regards & Thanks

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Okay. Here's the case.
In cluster, there's a special service called "web".

Only it can have the UI stuff. Other are just exposing DDP apis.
Since you don't have either "web", results will be unknown. (theoretically, it should not display anything)

@radiegtya
Copy link
Author

Sorry I mean the offline are web. And when I am changing to "web" the display within localhost:3000 and localhost:4000 will be same. (btw I had changed the code above)

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Yes. That's true. When the offline became the "web". If you visit any of the app, you'll get the UI of the web UI. That's the idea of the cluster.

@radiegtya
Copy link
Author

Oh I see, so in my case. It was incorrect if I choose cluster. Btw this is working when I am changing the cluster to normal DDP. Thanks for the answer @arunoda

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Yes. I will consider something like this in the future.
On 2015 පෙබ 18, බදාදා at පෙ.ව. 7.30 Ega Wachid Radiegtya <
[email protected]> wrote:

Oh I see, so in my case. It was incorrect if I choose cluster. Btw this is
working when I am changing the cluster to normal DDP. Thanks for the answer
@arunoda https://github.com/arunoda


Reply to this email directly or view it on GitHub
#15 (comment).

@radiegtya
Copy link
Author

Ok @arunoda. I will building something like core banking app. So I need the offline version for internal bank use. And the online version for customer access their balance etc. So I need offline version database and online version database. Both of them can share data. While offline connection are down, the app are still able to use the online connection vice versa.

For now I am still using DDP at online version to be connected to collection in offline version. When offline connection are down, the online db are using GroundDB to save it's state in local storage. When the offline connection are open again, the local storage from online version are synced to offline storage.

Regards

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Okay. I got. May be we need to have more UI services in a cluster. I will
make a change. May be today.
On 2015 පෙබ 18, බදාදා at පෙ.ව. 7.40 Ega Wachid Radiegtya <
[email protected]> wrote:

Ok @arunoda https://github.com/arunoda. I will building something like
core banking app. So I need the offline version for internal bank use. And
the online version for customer access their balance etc. So I need offline
version database and online version database. Both of them can share data.
While offline connection are down, the app are still able to use the online
connection vice versa.

For now I am still using DDP at online version to be connected to
collection in offline version. When offline connection are down, the online
db are using GroundDB to save it's state in local storage. When the offline
connection are open again, the local storage from online version are synced
to offline storage.

Regards


Reply to this email directly or view it on GitHub
#15 (comment).

@radiegtya
Copy link
Author

Cool, Thanks @arunoda

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Hi,

I released a new version with now you can have many UIs.
You don't have to do anything, just update it will work.

If you need more info, check the docs.

@arunoda arunoda reopened this Feb 18, 2015
@radiegtya
Copy link
Author

Now the UI are able to be viewed in each app. But I can't get the collection in my second app (empty array result).

here are the code:

/*=============== OFFLINE APP (localhost:3000) ===============*/
if (Meteor.isServer) {
    Cluster.connect("mongodb://localhost:27017/discovery");
    Cluster.register("web");
    // web app
    Meteor.publish('posts', function() {
        return Posts.find({});
    });
}
 /*=============== ONLINE APP (localhost:4000) ===============*/
if (Meteor.isServer) {
Cluster.connect("mongodb://localhost:27017/discovery");
Cluster.register("online");
var offlineConn = Cluster.discoverConnection('web');
offlineConn.subscribe('posts');
var Posts = new Mongo.Collection('posts', offlineConn);

    console.log(Posts.find().fetch()) //THIS PART PRINT EMPTY array []
}

Any Solutions?

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Yes. it seems like. Let's try to fix it.

On Wed Feb 18 2015 at 1:20:00 PM Ega Wachid Radiegtya <
[email protected]> wrote:

Now the UI are able to be viewed in each app. But I can't get the
collection in my second app (empty array result).

here are the code:

/=============== OFFLINE APP (localhost:3000) ===============/
if (Meteor.isServer) {
Cluster.connect("mongodb://localhost:27017/discovery");
Cluster.register("web");
// web app
Meteor.publish('posts', function() {
return Posts.find({});
});
}

/=============== ONLINE APP (localhost:4000) ===============/
if (Meteor.isServer) {
Cluster.connect("mongodb://localhost:27017/discovery");
Cluster.register("online");
var offlineConn = Cluster.discoverConnection('web');
offlineConn.subscribe('posts');
var Posts = new Mongo.Collection('posts', offlineConn);

console.log(Posts.find().fetch()) //THIS PART PRINT EMPTY array []

}

Any Solutions?


Reply to this email directly or view it on GitHub
#15 (comment).

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

I've a fix. I'm working on it now.

On Wed Feb 18 2015 at 1:47:46 PM Arunoda Susiripala <
[email protected]> wrote:

Yes. it seems like. Let's try to fix it.

On Wed Feb 18 2015 at 1:20:00 PM Ega Wachid Radiegtya <
[email protected]> wrote:

Now the UI are able to be viewed in each app. But I can't get the
collection in my second app (empty array result).

here are the code:

/=============== OFFLINE APP (localhost:3000) ===============/
if (Meteor.isServer) {
Cluster.connect("mongodb://localhost:27017/discovery");
Cluster.register("web");
// web app
Meteor.publish('posts', function() {
return Posts.find({});
});
}

/=============== ONLINE APP (localhost:4000) ===============/
if (Meteor.isServer) {
Cluster.connect("mongodb://localhost:27017/discovery");
Cluster.register("online");
var offlineConn = Cluster.discoverConnection('web');
offlineConn.subscribe('posts');
var Posts = new Mongo.Collection('posts', offlineConn);

console.log(Posts.find().fetch()) //THIS PART PRINT EMPTY array []

}

Any Solutions?


Reply to this email directly or view it on GitHub
#15 (comment).

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Okay now check with the new version.
This how you should write the code. Make sure to call the find() after the subscription has been made.

if (Meteor.isServer) {
  Cluster.connect("mongodb://localhost:27017/discovery");
  Cluster.register("online");
  var offlineConn = Cluster.discoverConnection('web');
  var Posts = new Mongo.Collection('posts', offlineConn);

  offlineConn.subscribe('posts', function() {
    console.log(Posts.find().fetch());
  });
}

@radiegtya
Copy link
Author

It work like charm although I don't use this

offlineConn.subscribe('posts', function() {
console.log(Posts.find().fetch());
});

But I have another question in mind, sorry about too many asking :D.
How if I want my "var Posts = new Mongo.Collection('posts', offlineConn);" is in the lib/both location and not in server location and make it global "Posts = new Mongo.Collection('posts', offlineConn);"? I am asking this because "Cluster" object are only able to be called on server. Another thing is to make realtime data call via client without using your package "Search Source".

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Actually, Cluster.discoverConnection('web') is available on the client as well. Then it will create a new connection to the web service from the offline service directly.

Todo that first, you need to call Cluster.allowPublicAccess("web"); on the offline app.

@radiegtya
Copy link
Author

When I am trying to place Cluster.discoverConnection('web') on client, it said "no such endpoint".

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Yes. You need to do this Cluster.allowPublicAccess("web"); on the offline app.

@radiegtya
Copy link
Author

I am also have doing that :D. but still no luck

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

If possible, send me a sample repo.
I'll see what's going on there.

On Wed Feb 18 2015 at 3:17:45 PM Ega Wachid Radiegtya <
[email protected]> wrote:

I am also have doing that :D. but still no luck


Reply to this email directly or view it on GitHub
#15 (comment).

@radiegtya
Copy link
Author

Here I made simple code repo, I decided to just using Search Packages of Yours.

https://github.com/radiegtya/cluster-online-offline

Please check it out :)

@arunoda
Copy link
Member

arunoda commented Feb 18, 2015

Thanks. I'll go through this and ping you.

On Wed Feb 18 2015 at 3:59:49 PM Ega Wachid Radiegtya <
[email protected]> wrote:

Here I made simple code repo, I decided to just using Search Packages of
Yours.

https://github.com/radiegtya/cluster-online-offline

Please check it out :)


Reply to this email directly or view it on GitHub
#15 (comment).

@radiegtya
Copy link
Author

Btw the problem now was I can't show the Posts data from the online app when using SearcSource package. Thanks @arunoda

@radiegtya
Copy link
Author

Hi @arunoda, btw I have solved the code. The app is now synced and able to show the data. I am forget to adding PostsSearch.search("text") when template rendered. Nah, the problem now is the data is not realtime/reactive. Is it possible to make SearchSource package being realtime (I think it's impossible because search source are running on server)? Thanks

@arunoda
Copy link
Member

arunoda commented Feb 20, 2015

Nope, search-source is not realtime. May be you need to poll for changes
from locally.

On Fri Feb 20 2015 at 9:40:19 AM Ega Wachid Radiegtya <
[email protected]> wrote:

Hi @arunoda https://github.com/arunoda, btw I have solved the code. The
app is now synced and able to show the data. I am forget to adding
PostsSearch.search("text") when template rendered. Nah, the problem now is
the data is not realtime/reactive. Is it possible to make SearchSource
package being realtime (I think it's impossible because search source are
running on server)? Thanks


Reply to this email directly or view it on GitHub
#15 (comment).

@radiegtya
Copy link
Author

Can You give me link about poll change in meteor?

and one another question (it's maybe out of the topic, but still related to meteor cluster)
It's about cluster and load balancer logic, Please take a look at my image below, is this correct?

arunoda meteor cluster logic explain

If You think I should ask this in another place, I'll move this :)
Sorry for asking too many question, but We are very interested to know more about cluster and load balancer in meteor. Ofc We can also promote your bulletproof site for Indonesian meteor community. This discussion is still very hot here. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants