Skip to main content

Methods API

Methods are Meteor’s remote procedure call (RPC) system, used to save data to the server and get return values. Source: packages/ddp-client/, packages/ddp-server/

Defining Methods

Meteor.methods()

Defines functions that can be invoked over the network by clients. Locus: Anywhere
methods
object
required
Dictionary of method functions by name.

Calling Methods

Meteor.call()

Invokes a method with a sync stub, passing any number of arguments. Locus: Anywhere
name
string
required
Name of method to invoke
...args
any
Optional method arguments
callback
function
Optional callback, which is called asynchronously with the error or result after the method is complete.

Meteor.callAsync()

Invokes a method with an async stub, passing any number of arguments. Returns a Promise. Locus: Anywhere
name
string
required
Name of method to invoke
...args
any
Optional method arguments
Returns: Promise

Meteor.apply()

Invoke a method passing an array of arguments. Locus: Anywhere
name
string
required
Name of method to invoke
args
array
required
Method arguments
options
object
Optional options object
wait
boolean
(Client only) If true, don’t send this method until all previous method calls have completed.
onResultReceived
function
(Client only) This callback is invoked with the error or result of the method as soon as the error or result is available.
noRetry
boolean
(Client only) If true, don’t send this method again on reload, simply call the callback an error with the error code ‘invocation-failed’.
throwStubExceptions
boolean
(Client only) If true, exceptions thrown by method stubs will be thrown instead of logged.
returnStubValue
boolean
(Client only) If true, return the stub’s return value instead of undefined.

Meteor.applyAsync()

Invoke a method passing an array of arguments, returning a Promise. Locus: Anywhere

Method Context (this)

Inside a method function, this is bound to a method invocation object with the following properties:
userId
string
The id of the user that made this method call, or null if no user was logged in.
connection
object
Access to the connection that called this method. null for server-initiated methods.
isSimulation
boolean
true if this invocation is a stub on the client, false if running on the server.
setUserId
function
(Server only) Set the logged-in user.
unblock
function
(Server only) Call this function to allow other methods from the same client to run while this method is still executing.

Method Simulation (Latency Compensation)

Methods defined on both client and server run on the client first (simulation/stub), then on the server.

Error Handling

Throwing Errors

Catching Errors

Validation

Always validate method arguments using the check package:

Complete Example