Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke a client js function in Meteor after getting results from the server

Tags:

meteor

I'm trying to see how can I invoke a js function after the client gets a result from a Meteor method call. The only thing I was able to get is to invoke the function myFunc only on the client that made the actual method call. Any thoughts how i can invoke the function on all the currently subscribed clients?

here is the code:

function myFunc(error, result)  {
  alert(result);
}
if (Meteor.is_client) {

  Template.container.events = {
    'click input' : function () {
      Meteor.call('someMethod',myFunc);
      if (typeof console !== 'undefined')
        console.log("You pressed the button");
    }
  };
}



if (Meteor.is_server) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

Meteor.methods({
  someMethod: function() {
    //console.log(!this.is_simulation);
    return "something";
  }
})

Thanks

like image 875
Gavriguy Avatar asked Apr 23 '12 04:04

Gavriguy


2 Answers

Currently you can't broadcast a method call to all clients directly. At least as far as I can tell. But a work around would be to create a collection called Alerts and monitor it for changes. Then when you want to send a message to all your users you can change the document in Alerts:

Client:

Alerts = new Meteor.Collection("alerts")

Meteor.autosubscribe(function() {
  Alerts.find().observe({
    added: function(item){ 
      alert(item.message);
    }
  });
});

Server:

Alerts = new Meteor.Collection("alerts")

Meteor.publish("alerts", function(){
 Alerts.find();
});

Alerts.remove({}); // remove all
Alerts.insert({message: "Some message to show on every client."});
like image 169
greggreg Avatar answered Oct 19 '22 08:10

greggreg


Another option is using Meteor Stream package which purpose is to avoid using a mongodb collection on the server side. It does supports client to clients, server to clients, client to server AND server to servers messaging, including a support for Meteor Cluster

If you want to stay with meteor only using collections, the following code allows you to either broadcast a message from the client to all the clients or a message from the server to all the subscribed clients. Just use this mechanism to then fire a function on the client side once the right message is received. The code is made in such a way that you will never have useless items remaining into the collection.

Messages = new Meteor.Collection("messages");

if (Meteor.isClient) {

    Meteor.subscribe("messages");

    var query = Messages.find({});
    var handle = query.observe({
        added: function(document)
        {
            console.log(document.message);
        }
    });

    // Test the mechanism from the client side
    Meteor.call("client talked");
}

if (Meteor.isServer) {
    Meteor.startup(function() {
        Messages.remove({});
    });

    Meteor.publish("messages", function()
    {
        // you might add an optional filter in order to broadcast only the messages you want to the client
        return Messages.find();
    });

    function talk(message)
    {
                    var id = Messages.insert({"message":message});
                    Messages.remove(id);
    }

    Meteor.methods(
            {
                talk: function(message)
                {
                    // you might filter here if the clients can talk using this.userId
                    talk(message);
                }
            });

    // test the mechanism from the server side
    talk("server talked");
}
like image 45
Flavien Volken Avatar answered Oct 19 '22 10:10

Flavien Volken