Skip to main content
Skip table of contents

Writing a Greeter Client in MATLAB

This page presents a step-by-step guide to writing the client-side of our MATLAB Greeter application.

This client is straightforward: it creates a proxy to a remote object that implements the Greeter interface, and invokes the greet operation on this object.

You can find the complete source code for this example in the ice-demos repository.

Compile Slice File with Slice Compiler

The first step when writing a MATLAB application with Ice is to compile the Slice definitions for this application with the Slice to MATLAB compiler (slice2matlab).

For our client application, we call slice2matlab on the Greeter.ice Slice file we wrote earlier:

BASH
slice2matlab Greeter.ice

This command generates the file +visitorcenter/GreeterPrx.m, which contains the proxy class used in our client code below.

Client Function

Our client is a small MATLAB function that loads the ice library:

MATLAB
function client(args)
    arguments (Repeating)
        args (1, :) char
    end

    if ~libisloaded('ice')
        loadlibrary('ice', @iceproto);
    end
    ...
end

The remainder of this function can be broken down into four pieces:

1. Create a Communicator

First, we create a Communicator using its constructor:

MATLAB
communicator = Ice.Communicator(args);

The communicator is the main entry point into the Ice runtime. Its responsibilities include establishing connections to servers, caching these connections, and managing configuration properties. We also need a communicator to create a proxy (see next step).

Our client, like most Ice applications, creates a single communicator.

When we no longer need a communicator, we must call destroy on this communicator. This destruction closes network connections and performs other important cleanups. We use the onCleanup function to schedule the call to the destroy method:

MATLAB
 cleanup = onCleanup(@() communicator.destroy());

2. Create a Greeter Proxy

Next, we need a way to call on a remote Greeter object. In Ice, this is done with proxies. Proxies are local constructs that represent remote Ice objects and provide methods to call operations on those objects.

We create a Greeter proxy by constructing an instance of the GreeterPrx class generated by the Slice compiler:

MATLAB
greeter = visitorcenter.GreeterPrx(communicator, ...
    'greeter:tcp -h hello.zeroc.com -p 4061');

The constructor accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says the target Ice object is named “greeter” and can be reached via tcp on hello.zeroc.com, on port 4061.

The name of the interface (Greeter) and the identity of the Ice object (greeter) are independent. The Ice objects hosted in the server could just as easily have identities like santa, bugsBunny, etc.

3. Make an Invocation

The third step is to call greet on the remote Ice object using our proxy, and print the greeting:

MATLAB
greeting = greeter.greet('alice');
fprintf('%s\n', greeting);

The greet method does all the heavy lifting for us: the proxy creates a request with the user name, the communicator establishes a connection to hello.zeroc.com:4061, and the request is sent over this connection. When a response is received, the proxy unmarshals its payload and returns a string (the greeting).

Here, we called the synchronous version of greet, which means this call will block until the response is received. Don’t let the simplicity of the syntax fool you: this is a remote call which will be much slower than a local call!

You can instead call greet asynchronously with greetAsync on the generated GreeterPrx class:

MATLAB
futureGreeting = greeter.greetAsync('bob');

greeting = futureGreeting.fetchOutputs();
fprintf('%s\n', greeting);

With the asynchronous version, the method call doesn’t block. Instead, the method returns a future immediately and we later poll this future (with fetchOutputs) to wait until the result is available.

Asynchronous invocations are more semantically correct for remote calls, and they alert readers to the potential delays inherent to these calls. But they are more complicated to write and are of limited value in MATLAB since we have to poll the returned future – MATLAB does not support the more elegant async/await syntax. In practice, making synchronous invocations is simpler and more common in MATLAB.

4. Cleanup

The final step is the end of our client function. At this point, the onCleanup calls destroy on the communicator, and our function completes.

Running the Client

We can run client directly in the MATLAB console:

CODE
client

Ice for MATLAB supports only client development.

Here, we connect to the Greeter server running on hello.zeroc.com. This Ice server is implemented in a language with server-side support (C++, C#, Java, Python, or Swift).

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.