Dude
Dude

Reputation: 1045

How can a server subscribe to a collection from the client?

I was wondering if it is possible that a server can subscribe to a collection from the clients so that only the server got everything and the users only have their local copy of the data.

I would like to create a meteor (mobile) app where people can create some content and store it in a collection (only local on the device) that noone can see except the "owner" itself locally and the server who owns everything. If a local collection is created the server subscribes to that collection so the server can collect all the data from every user and show it e.g. on a website. So I have 3 Components. The mobile app that generates the content (clients), the server that collects everything, and a website that only read the db with the complete content.

so, is it possible that a server can subscribe to every collection from every user?

Upvotes: 0

Views: 101

Answers (2)

255kb - Mockoon
255kb - Mockoon

Reputation: 6974

If you have some kind of authentication (or if you use the Accounts Meteor package) you can use Meteor.publish()on server side to only publish what belongs to the current user.

For example:

//Common Collection for both client and server (declared in lib/ for example): 
//(will be a full DB on the server and a minimongo with published data only 
//on the client)
Data = new Mongo.Collection('Data');    

//Server: (publish to client only what belongs to the user)
Meteor.publish('userData', function(){
    return Data.find({your_user_id_field:this.userId});
});

//Client: (the client only gets the data that belongs to him/her)
Meteor.subscribe('userData');
[... use the local collection as usual ...]

Upvotes: 1

Steffo
Steffo

Reputation: 622

Yes, but the idea is: every subscribed user (client) is allowed to see his data. Have a look at the collection.allow(options) documentation. A client subscribes to a collection and the server decides what the client can see.

Upvotes: 2

Related Questions