KJW
KJW

Reputation: 15251

Meteor: is Publish and Subscribe the only way to pass value between client and server side?

Basically, I need to initiate a background process when a user logs in. The background process returns some sensitive data, the server side should further process it and then make it available for the client.

Is this where Meteor.Publish and Subscribe methods come into play? Or do I need to use Meteor.methods? Are there any other approaches?

Upvotes: 1

Views: 734

Answers (1)

Tarang
Tarang

Reputation: 75975

For this kind of thing you might want to use a call instead of a publish. This is because the use case of the publish function is more to decide what the user should see & not really to process data (i.e do a web scrape or something and collect this) & the process might be blocking so all clients might to wait for this task to finish.

I would suggest you migrate over to meteorite: https://github.com/oortcloud/meteorite via

npm install -g meteorite

You would now have access to the wonderful collection of community packages at http://atmosphere.meteor.com.

Ted Blackman's event-horizon package lets you create events when the user logs in on the client.

You can then create an event for this:

Client Js

EventHorizon.fireWhenTrue('loggedIn',function(){
  return Meteor.userId() !== null;
});

EventHorizon.on('loggedIn',function(){
  Meteor.call("userloggedin", function(err,result) {
      console.log("Ready")
      if(result) {
          //do stuff that you wanted after the task is complete
      }
  }
});

Server js

Meteor.methods({
    'userloggedin' : function() { 
        this.unblock(); //unblocks the thread for long tasks
        //Do your stuff to Meteor.user();
        return true; //Tell the client the task is done.
    }
});

Upvotes: 3

Related Questions