Call js client function in Meteor after receiving results from the server

I am trying to understand how I can call the js function after the client receives the result of calling the Meteor method. The only thing I managed to get was to call the myFunc function only on the client who made the actual method call. Any thoughts on how I can call the function for all clients who have signed up at the moment?

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

+8
meteor
source share
4 answers

Currently, you cannot directly broadcast a method call to all clients. At least as far as I can tell. But the work around will be to create a collection called Alerts and track 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."}); 
+11
source share

Another option is to use the Meteor Stream package , whose purpose is to avoid using the server-side mongodb collection. It supports client-to-client, server-to-client, client-server and server-to-server messaging, including Meteor Cluster support

If you want to stay with the meteorite only through collections, the following code allows you to either send a message from the client to all clients, or a message from the server to all subscribers. Just use this mechanism to then run the function on the client side after receiving the correct message. The code is executed in such a way that you will never have any unnecessary items left in 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"); } 
+2
source share

I like what Zeke said, but for people who use Meteor 0.5.0+, use Deps.autorun instead of auto-subscribing ... more info: https://groups.google.com/forum/#!topic/meteor-core / mTa81RLvhbY as well as http://www.meteor.com/blog/2013/02/14/meteor-055-devshop-code-and-community-contributions

0
source share

A simple approach for calling a JavaScript client function would be to add a script tag to your html template linked by your collection. Each time a new element is inserted, this tag will be inserted into the client that will run your function. I have a call to the uploads collection with some properties like name . The following template starts the drawpoints () function on the client side after receiving a new item in the Uploads collection:

  {{#each uploads}} <tr> <td>{{name}}</td> <td> <div class="alert alert-success"><a href="{{url download=true}}">Download Here</a></div> </td> </tr> <script>drawpoints();</script> {{/each}} 
0
source share

All Articles