Main Content


Create ROS service server


Use rossvcserver or ros.ServiceServer to create a ROS service server that can receive requests from, and send responses to, a ROS service client. You must create the service server before creating the service client (see ROSSVCCLIENT).

When you create the service client, it establishes a connection to the server. The connection persists while both client and server exist and can reach each other. When you create the service server, it registers itself with the ROS master. To get a list of services, or to get information about a particular service that is available on the current ROS network, use the rosservice function.

The service has an associated message type and contains a pair of messages: one for the request and one for the response. The service server receives a request, constructs an appropriate response based on a call function, and returns it to the client. The behavior of the service server is inherently asynchronous because it becomes active only when a service client connects to the ROS network and issues a call.

Use the ros.ServiceServer syntax when connecting to a specific ROS node.




server = rossvcserver(servicename,svctype) creates a service server object with the specified ServiceType available in the ROS network under the name ServiceName. The service object cannot respond to service requests until you specify a function handle callback, NewMessageFcn.

server = rossvcserver(servicename,svctype,callback) specifies the callback function that constructs a response when the server receives a request. The callback specifies the NewMessageFcn property.

server = ros.ServiceServer(node, name,type) creates a service server that attaches to the ROS node, node. The server becomes available through the specified service name and type once a callback function handle is specified in NewMessageFcn.

server = ros.ServiceServer(node, name,type,callback) specifies the callback function, which is set to the NewMessageFcn property.


expand all

This property is read-only.

Name of the service, specified as a string scalar or character vector.

Example: "/gazebo/get_model_state"

Data Types: char | string

This property is read-only.

Type of service, specified as a string scalar or character vector.

Example: "gazebo_msgs/GetModelState"

Data Types: char | string

Callback property, specified as a function handle or cell array. In the first element of the cell array, specify either a function handle, string scalar, or character vector representing a function name. In subsequent elements, specify user data.

The service callback function requires at least three input arguments with one output. The first argument, src, is the associated service server object. The second argument, reqMsg, is the request message object sent by the service client. The third argument is the default response message object, defaultRespMsg. The callback returns a response message, response, based on the input request message and sends it back to the service client. Use the default response message as a starting point for constructing the request message. The function header for the callback is:

function response = serviceCallback(src,reqMsg,defaultRespMsg)

Specify the NewMessageFcn property as:

server.NewMessageFcn = @serviceCallback;

When setting the callback, you pass additional parameters to the callback function by including both the callback function and the parameters as elements of a cell array. The function header for the callback is:

function response = serviceCallback(src,reqMsg,defaultRespMsg,userData)

Specify the NewMessageFcn property as:

server.NewMessageFcn = {@serviceCallback,userData};

Object Functions

rosmessageCreate ROS messages


collapse all

Connect to a ROS network.

Launching ROS Core...
...................................................Done in 1.3941 seconds.
Initializing ROS master on
Initializing global node /matlab_global_node_52053 with NodeURI http://bat1002911glnxa64:33583/

Set up a service server and client.

server = rossvcserver('/test', 'std_srvs/Empty', @exampleHelperROSEmptyCallback);
client = rossvcclient('/test');

Call service server with default message.

response = call(client)
response = 
  ROS EmptyResponse message with properties:

    MessageType: 'std_srvs/EmptyResponse'

  Use showdetails to show the contents of the message

Shut down the ROS network.

Shutting down global node /matlab_global_node_52053 with NodeURI http://bat1002911glnxa64:33583/
Shutting down ROS master on
Introduced in R2019b