The RPC response object is passed to the callback registered with client.rpc.provide()
. It allows RPC providers to decide how to react to an incoming request.
Methods
response.send( data )
argument | type | optional | description |
---|---|---|---|
data | Mixed | false | Any serializable response data |
Succesfully complete a remote procedure call and send data back to the requesting client.
data
can be any kind of serializable data, e.g. Objects, Numbers, Booleans or Strings
client.rpc.provide( 'add-two-numbers', ( data, response ) => {
response.send( data.numA + data.numB );
});
response.reject()
Rejects the request. Rejections are not errors, but merely a means of saying "I'm busy at the moment, try another provider". Upon receiving a rejection deepstreamHub will try to re-route the request to another provider for the same RPC. If there are no more providers left to try, deepstream will send a NO_RPC_PROVIDER error to the client.
client.rpc.provide( 'add-two-numbers', ( data, response ) => {
//reject the response so that it gets
//re-routed to another provider
response.reject();
});
response.error( errorMsg )
argument | type | optional | description |
---|---|---|---|
errorMsg | String | false | A string that will be passed as error to the RPC requestor |
Send an error to the client. errorMsg
will be received as the first argument to the callback registered with client.rpc.make()
. This will complete the RPC.
client.rpc.provide( 'count-vote', ( data, response ) => {
if( hasAlreadyVoted( data.user ) ) {
response.error( 'You can only vote once' );
}
});
response.ack()
Explicitly acknowledges the receipt of a request.
This is usually done automatically, but can also be performed explicitly by setting response.autoAck = false
and calling ack()
later. This is useful when a client needs to perform an asynchronous operation to determine if it will accept or reject the request.
Info
- Requests count as completed once
send()
orerror()
was called. Callingack()
after that won't do anything.
client.rpc.provide( 'resize-image', ( data, response ) => {
// Turn of automatic acknowledgements. This needs to happen synchronously
response.autoAck = false;
// Acknowledge the request yourself at a later point
hasCapacities().then(() => {
response.ack();
});
});