---
title: "Writing a Greeter Client"
url: https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client
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

Parts of this page are for some programming languages only, and name them. For one language's text alone, see [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), [MATLAB](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/matlab.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), and [Swift](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/swift.md).

> **C++**
>
> This page provides a step-by-step guide to writing the client-side of our C++ Greeter application.
>
> This client 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/cpp/Ice/greeter). The code below is lightly simplified: it leaves out the demo’s error-reporting helper, and it passes fixed names to `greet` where the demo passes the name of the logged-in user.
>
> ## Compile Slice File with Slice Compiler
>
> The first step when writing a C++ application with Ice is to compile the Slice definitions for this application with the Slice to C++ compiler (`slice2cpp`).
>
> Here, we compile the `Greeter.ice` Slice file we wrote earlier:
>
> ```shell
> slice2cpp Greeter.ice
> ```
>
> This produces two files: a header file, `Greeter.h`, and a C++ source file, `Greeter.cpp`. The header file provides the `GreeterPrx` class we instantiate in the code below, and `Greeter.cpp` is compiled into the client like any other source file. See [Using the Slice Compiler](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/cpp.md) for the options `slice2cpp` accepts.
>
> The C++ demo programs build with [CMake](https://cmake.org/): the `slice2cpp_generate` function of the Ice CMake package compiles `Greeter.ice` as part of the build. See [Using Ice with CMake](https://docs.zeroc.com/ice/3.8/build-tools/cmake.md).
>
> ## Client Implementation
>
> The structure of our client is going to look like:
>
> ```cpp
> #include "Greeter.h"
>
> #include <Ice/Ice.h>
> #include <future>
> #include <iostream>
>
> using namespace std;
>
> int
> main(int argc, char* argv[])
> {
>     // ...
> }
> ```
>
> Before anything else, we need to include a few header files:
>
> - `Greeter.h`: The header file that the Slice compiler generated from `Greeter.ice`.
> - `Ice/Ice.h`: This header provides definitions that are necessary for accessing the Ice runtime.
> - `future`, `iostream`: Standard library headers we use in this client.
>
> Then we get to the interesting part: the `main` function which will run the client logic. This logic can be broken down into four pieces:
>
> ### 1. Create a Communicator
>
> First, we create a [Communicator](https://docs.zeroc.com/ice/3.8/runtime/communicator/cpp.md) with `Ice::initialize`:
>
> ```cpp
> Ice::CommunicatorPtr communicator = Ice::initialize(argc, argv);
> ```
>
> > **Tip**
> >
> > The `Ptr` types in the Ice for C++ API are all aliases for `std::shared_ptr`. For example:
> >
> > ```cpp
> > using CommunicatorPtr = std::shared_ptr<Communicator>;
> > ```
>
> 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. An easy way to do this is by placing the communicator in a [CommunicatorHolder](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1CommunicatorHolder.html). When the holder goes out of scope, its destructor calls `destroy` on the communicator:
>
> ```cpp
> Ice::CommunicatorHolder communicatorHolder{communicator};
> ```
>
> ### 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 functions to call operations on those objects.
>
> We create a `Greeter` proxy by calling the constructor of the `GreeterPrx` class generated by the Slice compiler:
>
> ```cpp
> VisitorCenter::GreeterPrx greeter{communicator, "greeter:tcp -h localhost -p 4061"};
> ```
>
> The constructor accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named `greeter` and can be reached via `tcp` on `localhost` on port `4061`. If you run the server on another computer, replace `localhost` with that computer’s hostname or IP address.
>
> > **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 to print the greeting:
>
> ```cpp
> string greeting = greeter.greet("alice");
> cout << greeting << endl;
> ```
>
> The `greet` function does all the heavy lifting for us: the proxy creates a request with the `name` string, the communicator establishes a connection to `localhost:4061`, and the request is sent over it. When a response is received, the proxy will unmarshal its payload and finally return a string (the greeting).
>
> > **Note**
> >
> > A proxy is a value, not a pointer, but `GreeterPrx` also provides `operator->`: `greeter.greet("alice")` and `greeter->greet("alice")` are equivalent. The demo source uses the `->` syntax.
>
> Here, we called the *synchronous* version of `greet`, which means this function call will block until the response is received. And 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 one of the two `greetAsync` overloads on the generated `GreeterPrx` class. The simpler overload returns a `future`:
>
> ```cpp
> future<string> futureGreeting = greeter.greetAsync("bob"); // Start the invocation.
>
> // Wait for the response.
> greeting = futureGreeting.get();
> cout << greeting << endl;
> ```
>
> `greetAsync` starts the invocation and returns a future, so the client can do other work before it needs the greeting. Calling `get` on the future then blocks until the response arrives.
>
> The other overload accepts callback functions instead of returning a future. The communicator calls these callbacks when it receives the response, or when it delivers an exception:
>
> ```cpp
> promise<void> promise;
> greeter.greetAsync(
>     "carol",
>     [&promise](string_view greeting) // response callback
>     {
>         cout << greeting << endl;
>         promise.set_value();
>     },
>     [&promise](std::exception_ptr exceptionPtr) // exception callback
>     { promise.set_exception(exceptionPtr); });
>
> // Wait for the response/exception callback to be called.
> promise.get_future().get();
> ```
>
> The callback overload is more flexible: you decide what runs when the response arrives, whereas a `std::future` gives you nowhere to attach a continuation, so the only thing you can do with it is wait. That flexibility costs you a fair amount of extra code, as you can see above.
>
> Asynchronous invocations are more semantically correct for remote calls, and they alert readers to the potential delays inherent to these calls. In C++ though, they’re significantly more complicated to write, so the best invocation syntax depends on your situation.
>
> ### 4. Cleanup
>
> The final step is the end of our main function. At this point, the `CommunicatorHolder` calls `destroy` on the communicator, and then our application exits.
>
> ```cpp
> return 0;
> ```
>
> ## Running the Client
>
> After building the client (see the demo’s [README](https://github.com/zeroc-ice/ice-demos/blob/3.8/cpp/Ice/greeter/README.md) for instructions), running it is as simple as running any other executable:
>
> > **Note**
> >
> > Start a Greeter server before you run the client — see [Writing a Greeter Server](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-server/cpp.md).
>
> **Linux and macOS:**
>
> ```shell
> ./build/client
> ```
>
> **Windows:**
>
> ```shell
> build\client
> ```
>
> With a server running, the client prints one greeting per invocation and exits:
>
> ```text
> Hello, alice!
> Hello, bob!
> Hello, carol!
> ```
>
> Our client doesn’t catch exceptions, to keep it short. If you run it without a server, the `greet` call fails with `Ice::ConnectionRefusedException` and the process terminates on an unhandled exception. A real client would catch `Ice::LocalException` around its invocations.

> **C#**
>
> This page presents a step-by-step guide to writing the client-side of our C# Greeter application.
>
> This client 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/csharp/Ice/Greeter).
>
> ## Compile Slice File with Slice Compiler
>
> The first step when writing a C# application with Ice is to compile the Slice definitions for this application with the Slice to C# compiler (`slice2cs`).
>
> To compile `Greeter.ice`, the demo adds the `ZeroC.Ice.Slice.Tools` NuGet package to the project and `Greeter.ice` as a `SliceCompile` item; MSBuild then runs `slice2cs` as part of every build. By default, the package adds every `.ice` file under the project directory as a `SliceCompile` item, but the demo keeps `Greeter.ice` in `../slice`, outside the project directory, so its project file adds it explicitly:
>
> ```xml title="Client.csproj"
> <ItemGroup>
>   <SliceCompile Include="../slice/Greeter.ice" />
>   <PackageReference Include="ZeroC.Ice" Version="3.8.*" />
>   <PackageReference Include="ZeroC.Ice.Slice.Tools" Version="3.8.*" PrivateAssets="all" />
> </ItemGroup>
> ```
>
> See [Slice Tools for Ice and C#](https://github.com/zeroc-ice/ice/blob/3.8/csharp/src/ZeroC.Ice.Slice.Tools/README.md) for the `SliceCompile` item metadata, and [Using the Slice Compiler](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/csharp.md) for the options of `slice2cs`.
>
> The build generates a single file named `Greeter.cs`, which provides the APIs that our client code calls.
>
> ## Client Implementation
>
> Since all the code generated by `slice2cs` was generated into the `VisitorCenter` namespace, we start with a using directive to let us reference it without qualification:
>
> ```csharp
> using VisitorCenter;
> ```
>
> Then the rest of the client logic can be broken down into four pieces:
>
> ### 1. Create a Communicator
>
> We create a [Communicator](https://code.zeroc.com/ice/3.8/api/csharp/api/Ice.Communicator.html) using its constructor:
>
> ```csharp
> await using var communicator = new Ice.Communicator(ref 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.
>
> It is important to make sure that your communicator is properly disposed when no longer needed. This ensures that network connections are gracefully closed, threads are joined, and other important clean-up occurs. The easiest way to do this is with an `await using` like we do here.
>
> ### 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 `createProxy` on the `GreeterPrxHelper` class generated by the Slice compiler. This returns a new instance of `GreeterPrx` (a `Greeter` proxy):
>
> ```csharp
> GreeterPrx greeter = GreeterPrxHelper.createProxy(
>     communicator,
>     "greeter:tcp -h localhost -p 4061");
> ```
>
> `createProxy` accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached via `tcp` on `localhost` 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 to print the greeting:
>
> ```csharp
> string greeting = await greeter.GreetAsync(Environment.UserName);
> Console.WriteLine(greeting);
> ```
>
> The `GreetAsync` method does all the heavy lifting for us: the proxy creates a request with the username string, the communicator establishes a connection to `localhost:4061`, and the request is sent over it. When a response is received, the proxy will unmarshal its payload and finally return a string (the greeting).
>
> Using `async`/`await` for this invocation offers a couple advantages:
>
> - The calling thread can continue doing other work while `GreetAsync` waits for I/O.
> - The `await` keyword signals to the reader that `GreetAsync` is a remote call that may take time to complete.
>
> ### 4. Cleanup
>
> Finally, at the end of our logic, our communicator goes out of scope and is disposed (because we used `await using`), and then our application exits.
>
> ## Running the Client
>
> After building the client (see the demo’s [README](https://github.com/zeroc-ice/ice-demos/blob/3.8/csharp/Ice/Greeter/README.md) for instructions), you can run it with `dotnet`:
>
> ```shell
> cd Client
> dotnet run
> ```
>
> > **Note**
> >
> > This client won’t work unless you’ve also launched a Greeter server — see [Writing a Greeter Server](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-server/csharp.md).

> **Java**
>
> This page presents a step-by-step guide to writing the client-side of our Java Greeter application.
>
> This client 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/java/Ice/greeter).
>
> ## Compile Slice File with Slice Compiler
>
> The first step when writing a Java application with Ice is to compile the Slice definitions for this application with the Slice to Java compiler (`slice2java`).
>
> To compile `Greeter.ice`, the demo adds the `com.zeroc.slice-tools` Gradle plugin to the project and the `../slice` directory to the Slice source set of `main`; Gradle then runs `slice2java` as part of every build:
>
> ```kotlin title="build.gradle.kts"
> plugins {
>     id("application")
>     id("com.zeroc.slice-tools") version "3.8.+"
> }
>
> dependencies {
>     implementation("com.zeroc:ice:3.8.+")
> }
>
> sourceSets {
>     main {
>         slice {
>             srcDirs("../slice")
>         }
>     }
> }
> ```
>
> See [Slice Tools for Ice and Java](https://github.com/zeroc-ice/ice/blob/3.8/java/tools/slice-tools/README.md) for the plugin settings, and [Using the Slice Compiler](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/java.md) for the options of `slice2java`.
>
> The build generates the `com.example.visitorcenter` package, which provides the `GreeterPrx` proxy interface that our client code uses.
>
> ## Client Implementation
>
> The structure of our client is going to look like:
>
> ```java
> package com.example.ice.greeter.client;
>
> import com.example.visitorcenter.GreeterPrx;
> import com.zeroc.Ice.Communicator;
>
> import java.util.concurrent.CompletableFuture;
> import java.util.concurrent.ExecutionException;
>
> class Client {
>     public static void main(String[] args) {
>         // ...
>     }
> }
> ```
>
> Before anything else, we import the following packages:
>
> - `com.example.visitorcenter`: The package that the Slice compiler (`slice2java`) generated from `Greeter.ice`.
> - `com.zeroc.Ice`: This package provides types that are necessary for accessing the Ice runtime.
> - `java.util.concurrent`: Standard library types we use in this client.
>
> Then we get to the interesting part: the `main` method which runs the client logic. This logic can be broken down into four pieces:
>
> ### 1. Create a Communicator
>
> First we create a [Communicator](https://code.zeroc.com/ice/3.8/api/java/com.zeroc.ice/com/zeroc/Ice/Communicator.html) using its constructor:
>
> ```java
> try (Communicator communicator = new Communicator(args)) {
>     // ... client code
> }
> ```
>
> 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.
>
> It is important to make sure that your communicator is properly closed when no longer needed. This ensures that network connections are gracefully closed, threads are joined, and other important clean-up occurs. The easiest way to do this is with a try-with-resources statement like we do here.
>
> ### 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 `createProxy` on the `GreeterPrx` interface generated by the Slice compiler. This returns a new instance of type `GreeterPrx` (a `Greeter` proxy):
>
> ```java
> var greeter = GreeterPrx.createProxy(
>     communicator, "greeter:tcp -h localhost -p 4061");
> ```
>
> `createProxy` accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached via `tcp` on `localhost` 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 to print the greeting:
>
> ```java
> String greeting = greeter.greet(System.getProperty("user.name"));
> System.out.println(greeting);
> ```
>
> The `greet` method does all the heavy lifting for us: the proxy creates a request with the username string, the communicator establishes a connection to `localhost:4061`, and the request is sent over it. When a response is received, the proxy will unmarshal its payload and finally return a string (the greeting).
>
> Here, we called the *synchronous* version of `greet`, which means this method call will block until the response is received. And 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 our proxy like this:
>
> ```java
> CompletableFuture<String> futureGreeting = greeter.greetAsync("alice");
>
> try {
>     greeting = futureGreeting.get();
>     System.out.println(greeting);
> } catch (InterruptedException | ExecutionException e) {
>     System.out.println("Could not get greeting: " + e.getMessage());
> }
> ```
>
> With the asynchronous version, the call doesn’t block. Instead the method returns a `CompletableFuture`. Calling `get` on this future waits for the invocation to complete and returns its result, as shown above.
>
> Asynchronous invocations are more semantically correct for remote calls, and they alert readers to the potential delays inherent to these calls. But in Java they’re slightly more complicated to write… as a result, the best invocation syntax depends on your situation.
>
> ### 4. Cleanup
>
> The final step is the end of our main method. At this point, our communicator goes out of scope and it is closed automatically (because we used a try-with-resources statement), and then our application exits.
>
> ## Running the Client
>
> After building the client (see the demo’s [README](https://github.com/zeroc-ice/ice-demos/blob/3.8/java/Ice/greeter/README.md) for instructions), you can run it with the launcher script that the build generates:
>
> ```shell
> ./client/build/install/client/bin/client
> ```
>
> > **Note**
> >
> > This client won’t work unless you’ve also launched a Greeter server — see [Writing a Greeter Server](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-server/java.md).

> **JavaScript**
>
> This page presents a step-by-step guide to writing the client-side of our TypeScript Greeter application, which runs in Node.js. For a client that runs in a web browser, see [Writing a Browser Greeter Client](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-browser-greeter-client.md).
>
> This client 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/js/Ice/greeter).
>
> ## Compile Slice File with Slice Compiler
>
> The first step when writing a TypeScript application with Ice is to compile the Slice definitions for this application with the Slice to JavaScript compiler (`slice2js`).
>
> Here, we compile the `Greeter.ice` Slice file we wrote earlier:
>
> ```shell
> npx slice2js --typescript Greeter.ice
> ```
>
> This produces a JavaScript module, `Greeter.js`, and, because we pass `--typescript`, a TypeScript declaration file, `Greeter.d.ts`. The declaration file describes the `GreeterPrx` class we instantiate in the code below, and the TypeScript compiler needs it to check our client code.
>
> We recommend that you include this Slice compilation step in your build. The `@zeroc/slice2js` package provides a plug-in that runs `slice2js` during a Vite, Rollup, webpack, or esbuild build. See [Using the Slice Compiler](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/js.md) for the plug-in and the `slice2js` options.
>
> ## Client Implementation
>
> The structure of our client is going to look like:
>
> ```typescript
> import { Ice } from "@zeroc/ice";
> import { VisitorCenter } from "./Greeter.js";
> import process from "node:process";
>
> // ...
> ```
>
> Before anything else, we need to import a few modules:
>
> - `Ice` is imported from the `@zeroc/ice` package and gives us access to the Ice runtime.
> - `VisitorCenter` is imported from the generated `Greeter.js` module, produced by the `slice2js` compiler.
> - `process` is imported from Node’s built-in process module and provides access to command-line arguments.
>
> Then we get to the interesting part: the client logic. We can break this logic down into four pieces:
>
> ### 1. Create a Communicator
>
> First, we create a [Communicator](https://code.zeroc.com/ice/3.8/api/javascript/Ice/Communicator.html) using its constructor:
>
> ```typescript
> await using communicator = new Ice.Communicator(process.argv);
> ```
>
> 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.
>
> It is important to make sure that your communicator is properly destroyed when no longer needed. This ensures that network connections are gracefully closed, and other important clean-up occurs. The easiest way to do this is with an `await using` like we do here.
>
> ### 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:
>
> ```typescript
> const greeter = new 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 that 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 to print the greeting:
>
> ```typescript
> const username = ...
> const greeting = await greeter.greet(username);
> console.log(greeting);
> ```
>
> The `greet` method does all the heavy lifting for us: the proxy creates a request with the username string, the communicator establishes a connection to `hello.zeroc.com:4061`, and the request is sent over it. When a response is received, the proxy will unmarshal its payload and finally return a string (the greeting).
>
> Note that `greet` returns a `Promise` that we await. This allows the JavaScript event loop to continue running other tasks while the invocation completes.
>
> ### 4. Cleanup
>
> Finally, at the end of our logic, our communicator goes out of scope and it is disposed automatically (because we used `await using`), and then our application exits.
>
> ## Running the Client
>
> After building the client (see the demo’s [README](https://github.com/zeroc-ice/ice-demos/blob/3.8/js/Ice/greeter/README.md) for instructions), you can run it with:
>
> ```shell
> node client.js
> ```
>
> > **Note**
> >
> > Ice for JavaScript has limited server side support.
> >
> > Here, we connect to the Greeter server running on `hello.zeroc.com`. This Ice server is implemented in a language with full server-side support (C++, C#, Java, Python, or Swift).

> **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](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).

> **PHP**
>
> This page presents a step-by-step guide to writing the client-side of our PHP Greeter application.
>
> This client 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/php/Ice/greeter).
>
> ## Compile Slice File with Slice Compiler
>
> The first step when writing a PHP application with Ice is to compile the Slice definitions for this application with the Slice to PHP compiler (`slice2php`).
>
> Here, we compile the `Greeter.ice` Slice file we wrote earlier:
>
> ```shell
> slice2php Greeter.ice
> ```
>
> This produces a single PHP source file named `Greeter.php`. This file provides the proxy class that we’ll use in our client code, so it’s essential to generate this file at the beginning of the development process.
>
> ## Client Script
>
> Our client is a small PHP script that loads the `Ice` library and the `Greeter.php` file generated by the Slice compiler:
>
> ```php
> <?php
>
> require_once 'Ice.php';
> require_once 'Greeter.php';
> ```
>
> The remainder of this script can be broken down into four pieces:
>
> ### 1. Create a Communicator
>
> First we create a Communicator using `Ice\initialize`:
>
> ```php
> $communicator = Ice\initialize($argv);
> ```
>
> 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.
>
> ### 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 `createProxy` on the `GreeterPrxHelper` class generated by the Slice compiler. This returns a new instance of a `Greeter` proxy.
>
> ```php
> $greeter = VisitorCenter\GreeterPrxHelper::createProxy(
>     $communicator, 'greeter:tcp -h hello.zeroc.com -p 4061');
> ```
>
> `createProxy` accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached over `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 to print the greeting:
>
> ```php
> $greeting = $greeter->greet("alice");
> echo "$greeting\n";
> ```
>
> The `greet` function does all the heavy lifting for us: the proxy creates a request with the user name (“Alice”), the communicator establishes a connection to `hello.zeroc.com:4061` and sends the request over this connection. Later the communicator receives the response, the proxy unmarshals the response’s payload and finally, returns a string (the greeting).
>
> Here, we make a synchronous invocation, which means this call blocks until the response is received. Ice for PHP supports only synchronous invocations; other languages support asynchronous invocations as well.
>
> ### 4. Cleanup
>
> At the end of the script, Ice destroys the communicator, which gracefully closes the connection to the server.
>
> ## Running the Client
>
> We can run this client script with PHP as follows:
>
> ```shell
> php Client.php
> ```
>
> > **Note**
> >
> > Ice for PHP 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).

> **Python**
>
> This page presents a step-by-step guide to writing the client-side of our Python Greeter application.
>
> This client 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](https://github.com/zeroc-ice/ice-demos/tree/3.8/python/Ice/greeter) repository on GitHub.
>
> ## Compile Slice File with Slice Compiler
>
> The first step when writing a Python application with Ice is to compile its Slice definitions using the Slice to Python compiler (`slice2py`).
>
> The demo is a [uv](https://docs.astral.sh/uv/) project. uv is a Python package and project manager: the demo’s `pyproject.toml` declares a dependency on the `zeroc-ice` package, which provides the Ice for Python runtime and the `slice2py` compiler. `uv run` runs a command in the project’s virtual environment, and creates this environment and installs the dependencies the first time you run it.
>
> Here we compile the `Greeter.ice` Slice file we wrote earlier, from the demo’s `client` directory:
>
> ```shell
> uv run slice2py ../slice/Greeter.ice
> ```
>
> Without uv, you install `zeroc-ice` with `pip` in a virtual environment, then run `slice2py ../slice/Greeter.ice` and `python main.py` in that environment.
>
> This compilation generates a Python package named `VisitorCenter`, which matches the Slice module name. Inside this package, you’ll find the generated `Greeter` module corresponding to the `Greeter` interface defined in Slice. This module provides the APIs that our client code will call, so generating it is an essential first step in the development process.
>
> ## Client Implementation
>
> The structure of our client is going to look like:
>
> ```py
> import asyncio
> import sys
>
> import Ice
>
> # Slice module VisitorCenter in Greeter.ice maps to Python module VisitorCenter.
> import VisitorCenter
>
>
> async def main():
>     ...
>
>
> if __name__ == "__main__":
>     asyncio.run(main())
> ```
>
> Before anything else, we need to import a few packages:
>
> - `asyncio` To write async code using `async/await` syntax.
> - `sys` For accessing the command line arguments.
> - `Ice` For accessing the Ice runtime.
> - `VisitorCenter` The package generated by `slice2py` from `Greeter.ice`.
>
> Then we get to the interesting part: the `main` coroutine, which `asyncio.run` runs in a new event loop. This logic can be broken down into four pieces:
>
> ### 1. Create a Communicator
>
> First, we create a [Communicator](https://code.zeroc.com/ice/3.8/api/python/Ice.Communicator.html) using its constructor:
>
> ```py
> async def main():
>     async with Ice.Communicator(
>         sys.argv, eventLoop=asyncio.get_running_loop()) as communicator:
> ```
>
> 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.
>
> We define main as an `async def` function to build an asynchronous application. By passing the current asyncio event loop to the `Ice.Communicator` constructor, we ensure that the communicator integrates with this loop for all asynchronous operations.
>
> It is important to always destroy the communicator before exiting the application. This guarantees that network connections are properly closed and other clean-up tasks are performed. In an asynchronous application, the simplest way to achieve this is to use the `async with` statement, as shown above.
>
> ### 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 functions to call operations on those objects.
>
> We create a `Greeter` proxy by calling the constructor of the `GreeterPrx` class generated by the Slice compiler:
>
> ```py
> greeter = VisitorCenter.GreeterPrx(communicator, "greeter:tcp -h localhost -p 4061")
> ```
>
> The constructor accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached via `tcp` on `localhost` 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 then print the returned greeting:
>
> ```py
> username = ...
> greeting = await greeter.greetAsync(username);
> print(greeting);
> ```
>
> The `greetAsync` function does all the heavy lifting for us: the proxy creates a request with the username string, the communicator establishes a connection to `localhost:4061`, and the request is sent over it. When a response is received, the proxy unmarshals its payload and returns a string (the greeting).
>
> Note that `greetAsync` returns an `Awaitable` object that we await. This allows the event loop thread to do other work while it is waiting for the invocation to complete.
>
> > **Note**
> >
> > Because the communicator was initialized with an asyncio event loop, the returned Awaitable is an `asyncio.Future`, which can be awaited within the same event loop.
>
> ### 4. Cleanup
>
> Finally, at the end of our logic, our communicator goes out of scope and is destroyed automatically (because we used the `async with` statement), and then our application exits.
>
> ## Running the Client
>
> After compiling the Slice definitions, you run the client from the demo’s `client` directory with:
>
> ```shell
> uv run main.py
> ```
>
> > **Note**
> >
> > This client won’t work unless you’ve also launched a Greeter server — see [Writing a Greeter Server](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-server/python.md).

> **Ruby**
>
> This page presents a step-by-step guide to writing the client-side of our Ruby Greeter application.
>
> This client 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/ruby/Ice/greeter).
>
> ## Compile Slice File with Slice Compiler
>
> The first step when writing a Ruby application with Ice is to compile the Slice definitions for this application with the Slice to Ruby compiler (`slice2rb`).
>
> Here, we compile the `Greeter.ice` Slice file we wrote earlier in a terminal:
>
> ```shell
> slice2rb Greeter.ice
> ```
>
> This compilation produces a Ruby source file, `Greeter.rb`. This file provides the proxy class that we’ll use in our client code, so it’s essential to generate this file at the beginning of the development process.
>
> ## Client Script
>
> Our client is a small Ruby script that loads the `Ice` library and the `Greeter.rb` file generated by the Slice compiler:
>
> ```ruby
> #!/usr/bin/env ruby
>
> require 'Ice'
>
> require_relative 'Greeter.rb'
> ```
>
> The remainder of this script can be broken down into four pieces:
>
> ### 1. Create a Communicator
>
> First, we create a Communicator using `Ice::initialize`:
>
> ```ruby
> Ice::initialize(ARGV) do |communicator|
>     ...
> end
> ```
>
> 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 Ruby’s “execute-around” pattern to destroy the communicator at the end of the block.
>
> ### 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:
>
> ```ruby
> greeter = VisitorCenter::GreeterPrx.new(
>     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:
>
> ```ruby
> greeting = greeter.greet("alice")
> puts greeting
> ```
>
> The `greet` method does all the heavy lifting for us: the proxy creates a request with the user name (“alice”), the communicator establishes a connection to `hello.zeroc.com:4061` and sends the request over this connection. Later, the communicator receives the response, the proxy unmarshals the response’s payload and returns a string (the greeting).
>
> Here, we make a synchronous invocation, which means this call blocks until we receive the greeting. Ice for Ruby supports only synchronous invocations; other languages support asynchronous invocations as well.
>
> ### 4. Cleanup
>
> The final step is the end of our block. At this point the communicator is destroyed, and our script completes.
>
> ## Running the Client
>
> We can now run this script in the Ruby console:
>
> ```shell
> ruby client.rb
> ```
>
> > **Note**
> >
> > Ice for Ruby 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).

> **Swift**
>
> This page presents a step-by-step guide to writing the client-side of our Swift Greeter application.
>
> This client 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/swift/Ice/Greeter).
>
> ## Compile Slice File with Slice Compiler
>
> To write a Swift application with Ice, first configure SwiftPM to compile the Slice definitions. The Ice package contains a plugin for this purpose, `CompileSlice`, which can be added to the executableTarget in `Package.swift`.
>
> ```swift
> .executableTarget(
>     name: "Client",
>     dependencies: [.product(name: "Ice", package: "ice")],
>     plugins: [.plugin(name: "CompileSlice", package: "ice")]
> ),
> ```
>
> The `CompileSlice` plugin compiles the `.ice` files in the target's sources, and the files listed in a `slice-plugin.json` file in the target's sources. In this example, `Greeter.ice` is in the `slice` directory at the root of the package, outside `Sources/Client`, so `Sources/Client/slice-plugin.json` lists it, with a path relative to the directory that contains `slice-plugin.json`:
>
> ```json
> {
>   "sources": ["../../slice/Greeter.ice"]
> }
> ```
>
> The plugin compiles `Greeter.ice` into `Greeter.swift` and adds it as a source file of the Client target. The generated code provides the APIs that we’ll call in our client code, so it’s an essential step of the development process.
>
> ## Client Implementation
>
> To get started we first need to import a few dependencies.
>
> ```swift
> import Foundation
> import Ice
> ```
>
> - `Foundation`: Used to obtain the username.
> - `Ice`: For accessing the Ice runtime
>
> Next is the client logic, which can be broken down into four pieces:
>
> ### 1. Create a Communicator
>
> First, we create a [Communicator](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/communicator) with `Ice.initialize`:
>
> ```swift
> var args = CommandLine.arguments
> let communicator = try Ice.initialize(&args)
>
> defer {
>     communicator.destroy()
> }
> ```
>
> 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.
>
> It is important to properly clean up the communicator when done, which we do with the defer block that calls `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 with the `makeProxy` function that the Slice compiler generates for the `GreeterPrx` protocol:
>
> ```swift
> let greeter = try makeProxy(
>     communicator: communicator, proxyString: "greeter:tcp -h localhost -p 4061",
>     type: GreeterPrx.self)
> ```
>
> The `makeProxy` function accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached via `tcp` on `localhost` 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 to print the greeting:
>
> ```swift
> let greeting = try await greeter.greet(NSUserName())
> print(greeting)
> ```
>
> The `greet` method does all the heavy lifting for us: the proxy creates a request with the username, the communicator establishes a connection to `localhost:4061`, and the request is sent over it. When a response is received, the proxy unmarshals its payload and returns a string (the greeting).
>
> Using `async`/`await` for this invocation offers several advantages:
>
> - The calling thread can continue doing other work while `greet` waits for I/O.
> - The `await` keyword signals to the reader that `greet` is a remote operation and may take time to complete.
>
> ### 4. Cleanup
>
> When the main function exits, the defer statement destroys the communicator.
>
> ## Running the Client
>
> To run the client, execute the following command (the executable will be compiled if necessary):
>
> ```shell
> swift run Client
> ```
>
> > **Note**
> >
> > This client won’t work unless you’ve also launched a [Greeter server](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-server/swift.md).
