Well, you are probably already asking yourself what is an RPC. Even if you have already googled the term, allow us to explain to you what an RPC is.
First of all, RPC stands for “Remote Procedure Call”, which basically means that you invoke “something” with some arguments ( only if you want to use arguments, of course). and you expect a response, just like you would do when in a conversation.
In Meteor, the usual and most recommended way of doing this, is by communicating through a websocket with the server.
So what actions we can do with RPC’s ?
- Fetching data
- Inserting Data
- Updating/Deleting Data
- Making API calls
- Anything you can possibly think of
Do not forget to
import '/imports/api/methods.js' in the server’s startup file:
/imports/startup/server/index.js otherwise meteor won’t know of the existence of this file
Methods can be called from the server-side or from the client-side. Let’s try it in the browser’s console:
To fully understand the reason why we added 2 arguments
result, go ahead and read the explanation here
Let’s cause an error and see what happens:
Type this code in the /imports/api/methods.js file:3
Now, make a method call in your browser’s console to
method.checkString and catch the exception: