---
title: "Writing a Greeter Client"
url: https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client
language: matlab
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Writing a Greeter Client

Programming language: MATLAB. Other languages: [C++](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/js.md), [PHP](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/php.md), [Python](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/swift.md).

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](https://github.com/zeroc-ice/ice-demos/tree/3.8/matlab/Ice/greeter).

## 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 the Ice toolbox's `slice2matlab` function on the `Greeter.ice` Slice file we wrote earlier, from the MATLAB console:

```matlab
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 calling the constructor 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`.

> **Note**
>
> 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. We call `fetchOutputs` once when we are ready to wait for the result; it blocks until the invocation completes.

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 wait on 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:

```matlab
client
```

> **Note**
>
> 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).
