What is the difference between publish / subscribe and method ? where/when we use method and publish /...












0















Today I try to work on meteor and getting an issue to get data from the server page..



I try to search it on google and meteor side but there is two way to get data publish / subscribe and Method



Have a below code, I didn't know how to write it on meteor server-side and get that data in client-side



function (x)
{
var y =2
var z = y*x
return z;
}


Now I want to call this method in client side










share|improve this question





























    0















    Today I try to work on meteor and getting an issue to get data from the server page..



    I try to search it on google and meteor side but there is two way to get data publish / subscribe and Method



    Have a below code, I didn't know how to write it on meteor server-side and get that data in client-side



    function (x)
    {
    var y =2
    var z = y*x
    return z;
    }


    Now I want to call this method in client side










    share|improve this question



























      0












      0








      0








      Today I try to work on meteor and getting an issue to get data from the server page..



      I try to search it on google and meteor side but there is two way to get data publish / subscribe and Method



      Have a below code, I didn't know how to write it on meteor server-side and get that data in client-side



      function (x)
      {
      var y =2
      var z = y*x
      return z;
      }


      Now I want to call this method in client side










      share|improve this question
















      Today I try to work on meteor and getting an issue to get data from the server page..



      I try to search it on google and meteor side but there is two way to get data publish / subscribe and Method



      Have a below code, I didn't know how to write it on meteor server-side and get that data in client-side



      function (x)
      {
      var y =2
      var z = y*x
      return z;
      }


      Now I want to call this method in client side







      meteor






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 20:16









      chazsolo

      5,4001234




      5,4001234










      asked Nov 23 '18 at 8:17









      Rawan-25Rawan-25

      205111




      205111
























          3 Answers
          3






          active

          oldest

          votes


















          0














          With publish/subscribe you keep track on datas collected.
          https://docs.meteor.com/api/pubsub.html



          According to the documentation:




          Methods are remote functions that Meteor clients can invoke with
          Meteor.call.




          So, to answer the last question: to call your function from client side, you have to use "Meteor.call('yourMethodName', optionalCallBacks);"



          EDIT:



          Ok, as suggested in the comments, here an example, with your function.
          In the server side, lets say on a file called "methods.js" or somethings:



          import { Meteor } from 'meteor/meteor';

          Meteor.methods({
          myMethod(x) {
          const y = 2;
          const z = y * x;
          return z;
          }
          });


          then in client side, you could call this mehod, like:



          Meteor.call('myMethod', x, (error, result) => {
          // OPTIONAL CALLBACK RESULT
          console.log(error, result);
          });


          Arguments here, are respectivelly, name of the method, variable named x, callback.






          share|improve this answer





















          • 1





            Please provide an example according to OP's code and correct the code formatting. Thank you.

            – Jankapunkt
            Nov 23 '18 at 16:54



















          0















          What is the difference between publish / subscribe and method ? where/when we use method and publish / subscribe?




          So if you want a logic you don't need pub/sub.
          Methods are for logic pub/sub for data handling.



          Important note:
          If you need to work with data from meteor's collection on the method you need to take into account this if method is executed on the server side it has access to all data (collections).
          If the method is executed on the client side it has access only to published data.



          On the other hand, according to your example, you don't need any data, so I'll skip it.



          I strongly suggest using validated methods:



          https://github.com/meteor/validated-method



          Now let's go to examples



          Imagine you have a method



          export const calculate = new ValidatedMethod({
          name: 'logic.calculate', // methods are usually named like this
          validate: new SimpleSchema({ // use SimpleSchema to autovalidate parameters
          x: {
          type: Number
          }
          }).validator(),
          run({ x }) {
          const y = 2;
          return y*x;
          }
          });


          Things to note:
          1. The file should be imported on the server somewhere.
          2. You need to use validation



          Now call it on the client



          Meteor.call('logic.calculate', { x }, (error, result) => {
          if (error) {
          do something
          }
          console.log(result);
          });


          Also, you can import the method directly and call it like this:



          import { calculate } from '../../api/logic/methods';// use real method path here   

          calculate.call({ x }, (error, result) => {
          if (error) {
          do something
          }
          console.log(result);
          });


          Note that for validated methods argument is an object






          share|improve this answer

































            0














            In meteor, we can create a meteor method and can pass data to client side by Meteor.call('methodName', 'param') . But in case of async operation we need to use future. Take a look on below example :



            Future = Npm.require('fibers/future');

            Meteor.methods({
            foo: function() {
            const future = new Future();
            someAsyncCall(foo, function bar(error, result) {
            if (error) future.throw(error);
            future.return(result);
            });
            // Execution is paused until callback arrives
            const ret = future.wait(); // Wait on future not Future
            return ret;
            }
            });





            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442895%2fwhat-is-the-difference-between-publish-subscribe-and-method-where-when-we-us%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              With publish/subscribe you keep track on datas collected.
              https://docs.meteor.com/api/pubsub.html



              According to the documentation:




              Methods are remote functions that Meteor clients can invoke with
              Meteor.call.




              So, to answer the last question: to call your function from client side, you have to use "Meteor.call('yourMethodName', optionalCallBacks);"



              EDIT:



              Ok, as suggested in the comments, here an example, with your function.
              In the server side, lets say on a file called "methods.js" or somethings:



              import { Meteor } from 'meteor/meteor';

              Meteor.methods({
              myMethod(x) {
              const y = 2;
              const z = y * x;
              return z;
              }
              });


              then in client side, you could call this mehod, like:



              Meteor.call('myMethod', x, (error, result) => {
              // OPTIONAL CALLBACK RESULT
              console.log(error, result);
              });


              Arguments here, are respectivelly, name of the method, variable named x, callback.






              share|improve this answer





















              • 1





                Please provide an example according to OP's code and correct the code formatting. Thank you.

                – Jankapunkt
                Nov 23 '18 at 16:54
















              0














              With publish/subscribe you keep track on datas collected.
              https://docs.meteor.com/api/pubsub.html



              According to the documentation:




              Methods are remote functions that Meteor clients can invoke with
              Meteor.call.




              So, to answer the last question: to call your function from client side, you have to use "Meteor.call('yourMethodName', optionalCallBacks);"



              EDIT:



              Ok, as suggested in the comments, here an example, with your function.
              In the server side, lets say on a file called "methods.js" or somethings:



              import { Meteor } from 'meteor/meteor';

              Meteor.methods({
              myMethod(x) {
              const y = 2;
              const z = y * x;
              return z;
              }
              });


              then in client side, you could call this mehod, like:



              Meteor.call('myMethod', x, (error, result) => {
              // OPTIONAL CALLBACK RESULT
              console.log(error, result);
              });


              Arguments here, are respectivelly, name of the method, variable named x, callback.






              share|improve this answer





















              • 1





                Please provide an example according to OP's code and correct the code formatting. Thank you.

                – Jankapunkt
                Nov 23 '18 at 16:54














              0












              0








              0







              With publish/subscribe you keep track on datas collected.
              https://docs.meteor.com/api/pubsub.html



              According to the documentation:




              Methods are remote functions that Meteor clients can invoke with
              Meteor.call.




              So, to answer the last question: to call your function from client side, you have to use "Meteor.call('yourMethodName', optionalCallBacks);"



              EDIT:



              Ok, as suggested in the comments, here an example, with your function.
              In the server side, lets say on a file called "methods.js" or somethings:



              import { Meteor } from 'meteor/meteor';

              Meteor.methods({
              myMethod(x) {
              const y = 2;
              const z = y * x;
              return z;
              }
              });


              then in client side, you could call this mehod, like:



              Meteor.call('myMethod', x, (error, result) => {
              // OPTIONAL CALLBACK RESULT
              console.log(error, result);
              });


              Arguments here, are respectivelly, name of the method, variable named x, callback.






              share|improve this answer















              With publish/subscribe you keep track on datas collected.
              https://docs.meteor.com/api/pubsub.html



              According to the documentation:




              Methods are remote functions that Meteor clients can invoke with
              Meteor.call.




              So, to answer the last question: to call your function from client side, you have to use "Meteor.call('yourMethodName', optionalCallBacks);"



              EDIT:



              Ok, as suggested in the comments, here an example, with your function.
              In the server side, lets say on a file called "methods.js" or somethings:



              import { Meteor } from 'meteor/meteor';

              Meteor.methods({
              myMethod(x) {
              const y = 2;
              const z = y * x;
              return z;
              }
              });


              then in client side, you could call this mehod, like:



              Meteor.call('myMethod', x, (error, result) => {
              // OPTIONAL CALLBACK RESULT
              console.log(error, result);
              });


              Arguments here, are respectivelly, name of the method, variable named x, callback.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 23 '18 at 19:14

























              answered Nov 23 '18 at 8:37









              Jack VabreJack Vabre

              2117




              2117








              • 1





                Please provide an example according to OP's code and correct the code formatting. Thank you.

                – Jankapunkt
                Nov 23 '18 at 16:54














              • 1





                Please provide an example according to OP's code and correct the code formatting. Thank you.

                – Jankapunkt
                Nov 23 '18 at 16:54








              1




              1





              Please provide an example according to OP's code and correct the code formatting. Thank you.

              – Jankapunkt
              Nov 23 '18 at 16:54





              Please provide an example according to OP's code and correct the code formatting. Thank you.

              – Jankapunkt
              Nov 23 '18 at 16:54













              0















              What is the difference between publish / subscribe and method ? where/when we use method and publish / subscribe?




              So if you want a logic you don't need pub/sub.
              Methods are for logic pub/sub for data handling.



              Important note:
              If you need to work with data from meteor's collection on the method you need to take into account this if method is executed on the server side it has access to all data (collections).
              If the method is executed on the client side it has access only to published data.



              On the other hand, according to your example, you don't need any data, so I'll skip it.



              I strongly suggest using validated methods:



              https://github.com/meteor/validated-method



              Now let's go to examples



              Imagine you have a method



              export const calculate = new ValidatedMethod({
              name: 'logic.calculate', // methods are usually named like this
              validate: new SimpleSchema({ // use SimpleSchema to autovalidate parameters
              x: {
              type: Number
              }
              }).validator(),
              run({ x }) {
              const y = 2;
              return y*x;
              }
              });


              Things to note:
              1. The file should be imported on the server somewhere.
              2. You need to use validation



              Now call it on the client



              Meteor.call('logic.calculate', { x }, (error, result) => {
              if (error) {
              do something
              }
              console.log(result);
              });


              Also, you can import the method directly and call it like this:



              import { calculate } from '../../api/logic/methods';// use real method path here   

              calculate.call({ x }, (error, result) => {
              if (error) {
              do something
              }
              console.log(result);
              });


              Note that for validated methods argument is an object






              share|improve this answer






























                0















                What is the difference between publish / subscribe and method ? where/when we use method and publish / subscribe?




                So if you want a logic you don't need pub/sub.
                Methods are for logic pub/sub for data handling.



                Important note:
                If you need to work with data from meteor's collection on the method you need to take into account this if method is executed on the server side it has access to all data (collections).
                If the method is executed on the client side it has access only to published data.



                On the other hand, according to your example, you don't need any data, so I'll skip it.



                I strongly suggest using validated methods:



                https://github.com/meteor/validated-method



                Now let's go to examples



                Imagine you have a method



                export const calculate = new ValidatedMethod({
                name: 'logic.calculate', // methods are usually named like this
                validate: new SimpleSchema({ // use SimpleSchema to autovalidate parameters
                x: {
                type: Number
                }
                }).validator(),
                run({ x }) {
                const y = 2;
                return y*x;
                }
                });


                Things to note:
                1. The file should be imported on the server somewhere.
                2. You need to use validation



                Now call it on the client



                Meteor.call('logic.calculate', { x }, (error, result) => {
                if (error) {
                do something
                }
                console.log(result);
                });


                Also, you can import the method directly and call it like this:



                import { calculate } from '../../api/logic/methods';// use real method path here   

                calculate.call({ x }, (error, result) => {
                if (error) {
                do something
                }
                console.log(result);
                });


                Note that for validated methods argument is an object






                share|improve this answer




























                  0












                  0








                  0








                  What is the difference between publish / subscribe and method ? where/when we use method and publish / subscribe?




                  So if you want a logic you don't need pub/sub.
                  Methods are for logic pub/sub for data handling.



                  Important note:
                  If you need to work with data from meteor's collection on the method you need to take into account this if method is executed on the server side it has access to all data (collections).
                  If the method is executed on the client side it has access only to published data.



                  On the other hand, according to your example, you don't need any data, so I'll skip it.



                  I strongly suggest using validated methods:



                  https://github.com/meteor/validated-method



                  Now let's go to examples



                  Imagine you have a method



                  export const calculate = new ValidatedMethod({
                  name: 'logic.calculate', // methods are usually named like this
                  validate: new SimpleSchema({ // use SimpleSchema to autovalidate parameters
                  x: {
                  type: Number
                  }
                  }).validator(),
                  run({ x }) {
                  const y = 2;
                  return y*x;
                  }
                  });


                  Things to note:
                  1. The file should be imported on the server somewhere.
                  2. You need to use validation



                  Now call it on the client



                  Meteor.call('logic.calculate', { x }, (error, result) => {
                  if (error) {
                  do something
                  }
                  console.log(result);
                  });


                  Also, you can import the method directly and call it like this:



                  import { calculate } from '../../api/logic/methods';// use real method path here   

                  calculate.call({ x }, (error, result) => {
                  if (error) {
                  do something
                  }
                  console.log(result);
                  });


                  Note that for validated methods argument is an object






                  share|improve this answer
















                  What is the difference between publish / subscribe and method ? where/when we use method and publish / subscribe?




                  So if you want a logic you don't need pub/sub.
                  Methods are for logic pub/sub for data handling.



                  Important note:
                  If you need to work with data from meteor's collection on the method you need to take into account this if method is executed on the server side it has access to all data (collections).
                  If the method is executed on the client side it has access only to published data.



                  On the other hand, according to your example, you don't need any data, so I'll skip it.



                  I strongly suggest using validated methods:



                  https://github.com/meteor/validated-method



                  Now let's go to examples



                  Imagine you have a method



                  export const calculate = new ValidatedMethod({
                  name: 'logic.calculate', // methods are usually named like this
                  validate: new SimpleSchema({ // use SimpleSchema to autovalidate parameters
                  x: {
                  type: Number
                  }
                  }).validator(),
                  run({ x }) {
                  const y = 2;
                  return y*x;
                  }
                  });


                  Things to note:
                  1. The file should be imported on the server somewhere.
                  2. You need to use validation



                  Now call it on the client



                  Meteor.call('logic.calculate', { x }, (error, result) => {
                  if (error) {
                  do something
                  }
                  console.log(result);
                  });


                  Also, you can import the method directly and call it like this:



                  import { calculate } from '../../api/logic/methods';// use real method path here   

                  calculate.call({ x }, (error, result) => {
                  if (error) {
                  do something
                  }
                  console.log(result);
                  });


                  Note that for validated methods argument is an object







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 13:47

























                  answered Nov 25 '18 at 13:42









                  ivan133ivan133

                  618920




                  618920























                      0














                      In meteor, we can create a meteor method and can pass data to client side by Meteor.call('methodName', 'param') . But in case of async operation we need to use future. Take a look on below example :



                      Future = Npm.require('fibers/future');

                      Meteor.methods({
                      foo: function() {
                      const future = new Future();
                      someAsyncCall(foo, function bar(error, result) {
                      if (error) future.throw(error);
                      future.return(result);
                      });
                      // Execution is paused until callback arrives
                      const ret = future.wait(); // Wait on future not Future
                      return ret;
                      }
                      });





                      share|improve this answer




























                        0














                        In meteor, we can create a meteor method and can pass data to client side by Meteor.call('methodName', 'param') . But in case of async operation we need to use future. Take a look on below example :



                        Future = Npm.require('fibers/future');

                        Meteor.methods({
                        foo: function() {
                        const future = new Future();
                        someAsyncCall(foo, function bar(error, result) {
                        if (error) future.throw(error);
                        future.return(result);
                        });
                        // Execution is paused until callback arrives
                        const ret = future.wait(); // Wait on future not Future
                        return ret;
                        }
                        });





                        share|improve this answer


























                          0












                          0








                          0







                          In meteor, we can create a meteor method and can pass data to client side by Meteor.call('methodName', 'param') . But in case of async operation we need to use future. Take a look on below example :



                          Future = Npm.require('fibers/future');

                          Meteor.methods({
                          foo: function() {
                          const future = new Future();
                          someAsyncCall(foo, function bar(error, result) {
                          if (error) future.throw(error);
                          future.return(result);
                          });
                          // Execution is paused until callback arrives
                          const ret = future.wait(); // Wait on future not Future
                          return ret;
                          }
                          });





                          share|improve this answer













                          In meteor, we can create a meteor method and can pass data to client side by Meteor.call('methodName', 'param') . But in case of async operation we need to use future. Take a look on below example :



                          Future = Npm.require('fibers/future');

                          Meteor.methods({
                          foo: function() {
                          const future = new Future();
                          someAsyncCall(foo, function bar(error, result) {
                          if (error) future.throw(error);
                          future.return(result);
                          });
                          // Execution is paused until callback arrives
                          const ret = future.wait(); // Wait on future not Future
                          return ret;
                          }
                          });






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 29 '18 at 17:29









                          Sourabh BhutaniSourabh Bhutani

                          164211




                          164211






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442895%2fwhat-is-the-difference-between-publish-subscribe-and-method-where-when-we-us%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud

                              Zucchini