To run this example you should have [Cocoapods](https://cocoapods.org/#install) installed, as well as the relevant tools to generate the client library code (and a server in another language, for testing). You can obtain the latter by following [these setup instructions](https://github.com/grpc/homebrew-grpc).
To run this example you should have [Cocoapods](https://cocoapods.org/#install) installed, as well
as the relevant tools to generate the client library code (and a server in another language, for
testing). You can obtain the latter by following [these setup instructions](https://github.com/grpc/homebrew-grpc).
## Hello Objective-C gRPC!
Here's how to build and run the Objective-C implementation of the [Hello World](https://github.com/grpc/grpc-common/blob/master/protos/helloworld.proto) example used in [Getting started](https://github.com/grpc/grpc-common).
Here's how to build and run the Objective-C implementation of the [Hello World](https://github.com/grpc/grpc-common/blob/master/protos/helloworld.proto)
example used in [Getting started](https://github.com/grpc/grpc-common).
The example code for this and our other examples lives in the `grpc-common`
GitHub repository. Clone this repository to your local machine by running the
following command:
The example code for this and our other examples lives in the `grpc-common` GitHub repository. Clone
this repository to your local machine by running the following command:
```sh
@ -24,7 +26,8 @@ $ cd grpc-common/objective-c/helloworld
```
### Try it!
To try the sample app, we need a gRPC server running locally. Let's compile and run, for example, the C++ server in this repository:
To try the sample app, we need a gRPC server running locally. Let's compile and run, for example,
the C++ server in this repository:
```shell
$ pushd ../../cpp/helloworld
@ -39,12 +42,15 @@ Now have Cocoapods generate and install the client library for our .proto files:
$ pod install
```
This might have to compile OpenSSL, which takes around 15 minutes if Cocoapods doesn't have it yet on your computer's cache).
(This might have to compile OpenSSL, which takes around 15 minutes if Cocoapods doesn't have it yet
on your computer's cache.)
Finally, open the XCode workspace created by Cocoapods, and run the app. You can check the calling code in `main.m` and see the results in XCode's log console.
Finally, open the XCode workspace created by Cocoapods, and run the app. You can check the calling
code in `main.m` and see the results in XCode's log console.
The code sends a `HLWHelloRequest` containing the string "Objective-C" to a local server. The server responds with a `HLWHelloResponse`, which contains a string that is then output to the log.
The code sends a `HLWHelloRequest` containing the string "Objective-C" to a local server. The server
responds with a `HLWHelloResponse`, which contains a string that is then output to the log.
## Tutorial
You can find a more detailed tutorial in [gRPC Basics: Objective-C](https://github.com/grpc/grpc-common/blob/master/objective-c/route_guide/README.md)
You can find a more detailed tutorial in [gRPC Basics: Objective-C](https://github.com/grpc/grpc-common/blob/master/objective-c/route_guide/README.md).
This tutorial provides a basic Objective-C programmer's introduction to working with gRPC. By walking through this example you'll learn how to:
This tutorial provides a basic Objective-C programmer's introduction to working with gRPC. By
walking through this example you'll learn how to:
- Define a service in a .proto file.
- Generate client code using the protocol buffer compiler.
- Use the Objective-C gRPC API to write a simple client for your service.
It assumes a passing familiarity with [protocol buffers](https://developers.google.com/protocol-buffers/docs/overview). Note that the example in this tutorial uses the proto3 version of the protocol buffers language, which is currently in alpha release: you can find out more in the [proto3 language guide](https://developers.google.com/protocol-buffers/docs/proto3) and see the [release notes](https://github.com/google/protobuf/releases) for the new version in the protocol buffers Github repository.
It assumes a passing familiarity with [protocol buffers](https://developers.google.com/protocol-buffers/docs/overview).
Note that the example in this tutorial uses the proto3 version of the protocol buffers language,
which is currently in alpha release: you can find out more in the [proto3 language guide](https://developers.google.com/protocol-buffers/docs/proto3)
and see the [release notes](https://github.com/google/protobuf/releases) for the new version in the
protocol buffers Github repository.
This isn't a comprehensive guide to using gRPC in Objective-C: more reference documentation is coming soon.
This isn't a comprehensive guide to using gRPC in Objective-C: more reference documentation is
coming soon.
- [Why use gRPC?](#why-grpc)
- [Example code and setup](#setup)
@ -20,15 +26,25 @@ This isn't a comprehensive guide to using gRPC in Objective-C: more reference do
<aname="why-grpc"></a>
## Why use gRPC?
With gRPC you can define your service once in a .proto file and implement clients and servers in any of gRPC's supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. You also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating.
With gRPC you can define your service once in a .proto file and implement clients and servers in any
of gRPC's supported languages, which in turn can be run in environments ranging from servers inside
Google to your own tablet - all the complexity of communication between different languages and
environments is handled for you by gRPC. You also get all the advantages of working with protocol
buffers, including efficient serialization, a simple IDL, and easy interface updating.
gRPC and proto3 are specially suited for mobile clients: gRPC is implemented on top of HTTP/2, which results in network bandwidth savings over using HTTP/1.1. Serialization and parsing of the proto binary format is more efficient than the equivalent JSON, resulting in CPU and battery savings. And proto3 uses a runtime that has been optimized over the years at Google to keep code size to a minimum. The latter is important in Objective-C, because the ability of the compiler to strip unused code is limited by the dynamic nature of the language.
gRPC and proto3 are specially suited for mobile clients: gRPC is implemented on top of HTTP/2, which
results in network bandwidth savings over using HTTP/1.1. Serialization and parsing of the proto
binary format is more efficient than the equivalent JSON, resulting in CPU and battery savings. And
proto3 uses a runtime that has been optimized over the years at Google to keep code size to a
minimum. The latter is important in Objective-C, because the ability of the compiler to strip unused
code is limited by the dynamic nature of the language.
<aname="setup"></a>
## Example code and setup
The example code for our tutorial is in [grpc/grpc-common/objective-c/route_guide](https://github.com/grpc/grpc-common/tree/master/objective-c/route_guide). To download the example, clone the `grpc-common` repository by running the following command:
The example code for our tutorial is in [grpc/grpc-common/objective-c/route_guide](https://github.com/grpc/grpc-common/tree/master/objective-c/route_guide).
To download the example, clone the `grpc-common` repository by running the following command:
@ -38,15 +54,20 @@ Then change your current directory to `grpc-common/objective-c/route_guide`:
$ cd grpc-common/objective-c/route_guide
```
Our example is a simple route mapping application that lets clients get information about features on their route, create a summary of their route, and exchange route information such as traffic updates with the server and other clients.
Our example is a simple route mapping application that lets clients get information about features
on their route, create a summary of their route, and exchange route information such as traffic
updates with the server and other clients.
You also should have [Cocoapods](https://cocoapods.org/#install) installed, as well as the relevant tools to generate the client library code (and a server in another language, for testing). You can obtain the latter by following [these setup instructions](https://github.com/grpc/homebrew-grpc).
You also should have [Cocoapods](https://cocoapods.org/#install) installed, as well as the relevant
tools to generate the client library code (and a server in another language, for testing). You can
obtain the latter by following [these setup instructions](https://github.com/grpc/homebrew-grpc).
<aname="try"></a>
## Try it out!
To try the sample app, we need a gRPC server running locally. Let's compile and run, for example, the C++ server in this repository:
To try the sample app, we need a gRPC server running locally. Let's compile and run, for example,
the C++ server in this repository:
```shell
$ pushd ../../cpp/route_guide
@ -61,17 +82,22 @@ Now have Cocoapods generate and install the client library for our .proto files:
$ pod install
```
(This might have to compile OpenSSL, which takes around 15 minutes if Cocoapods doesn't have it yet on your computer's cache).
(This might have to compile OpenSSL, which takes around 15 minutes if Cocoapods doesn't have it yet
on your computer's cache).
Finally, open the XCode workspace created by Cocoapods, and run the app. You can check the calling code in `ViewControllers.m` and see the results in XCode's log console.
Finally, open the XCode workspace created by Cocoapods, and run the app. You can check the calling
code in `ViewControllers.m` and see the results in XCode's log console.
The next sections guide you step-by-step through how this proto service is defined, how to generate a client library from it, and how to create an app that uses that library.
The next sections guide you step-by-step through how this proto service is defined, how to generate
a client library from it, and how to create an app that uses that library.
<aname="proto"></a>
## Defining the service
First let's look at how the service we're using is defined. A gRPC *service* and its method *request* and *response* types using [protocol buffers](https://developers.google.com/protocol-buffers/docs/overview). You can see the complete .proto file for our example in [`grpc-common/protos/route_guide.proto`](https://github.com/grpc/grpc-common/blob/master/protos/route_guide.proto).
First let's look at how the service we're using is defined. A gRPC *service* and its method
*request* and *response* types using [protocol buffers](https://developers.google.com/protocol-buffers/docs/overview).
You can see the complete .proto file for our example in [`grpc-common/protos/route_guide.proto`](https://github.com/grpc/grpc-common/blob/master/protos/route_guide.proto).
To define a service, you specify a named `service` in your .proto file:
@ -81,15 +107,20 @@ service RouteGuide {
}
```
Then you define `rpc` methods inside your service definition, specifying their request and response types. Protocol buffers let you define four kinds of service method, all of which are used in the `RouteGuide` service:
Then you define `rpc` methods inside your service definition, specifying their request and response
types. Protocol buffers let you define four kinds of service method, all of which are used in the
`RouteGuide` service:
- A *simple RPC* where the client sends a request to the server and receives a response later, just like a normal remote procedure call.
- A *simple RPC* where the client sends a request to the server and receives a response later, just
like a normal remote procedure call.
```protobuf
// Obtains the feature at a given position.
rpc GetFeature(Point) returns (Feature) {}
```
- A *response-streaming RPC* where the client sends a request to the server and gets back a stream of response messages. You specify a response-streaming method by placing the `stream` keyword before the *response* type.
- A *response-streaming RPC* where the client sends a request to the server and gets back a stream
of response messages. You specify a response-streaming method by placing the `stream` keyword before
the *response* type.
```protobuf
// Obtains the Features available within the given Rectangle. Results are
// streamed rather than returned at once (e.g. in a response message with a
@ -98,21 +129,30 @@ Then you define `rpc` methods inside your service definition, specifying their r
- A *request-streaming RPC* where the client sends a sequence of messages to the server. Once the client has finished writing the messages, it waits for the server to read them all and return its response. You specify a request-streaming method by placing the `stream` keyword before the *request* type.
- A *request-streaming RPC* where the client sends a sequence of messages to the server. Once the
client has finished writing the messages, it waits for the server to read them all and return its
response. You specify a request-streaming method by placing the `stream` keyword before the
*request* type.
```protobuf
// Accepts a stream of Points on a route being traversed, returning a
- A *bidirectional streaming RPC* where both sides send a sequence of messages to the other. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes. The order of messages in each stream is preserved. You specify this type of method by placing the `stream` keyword before both the request and the response.
- A *bidirectional streaming RPC* where both sides send a sequence of messages to the other. The two
streams operate independently, so clients and servers can read and write in whatever order they
like: for example, the server could wait to receive all the client messages before writing its
responses, or it could alternately read a message then write a message, or some other combination of
reads and writes. The order of messages in each stream is preserved. You specify this type of method
by placing the `stream` keyword before both the request and the response.
```protobuf
// Accepts a stream of RouteNotes sent while a route is being traversed,
// while receiving other RouteNotes (e.g. from other users).
Our .proto file also contains protocol buffer message type definitions for all the request and response types used in our service methods - for example, here's the `Point` message type:
Our .proto file also contains protocol buffer message type definitions for all the request and
response types used in our service methods - for example, here's the `Point` message type:
```protobuf
// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest integer).
@ -124,7 +164,8 @@ message Point {
}
```
You can specify a prefix to be used for your generated classes by adding the `objc_class_prefix` option at the top of the file. For example:
You can specify a prefix to be used for your generated classes by adding the `objc_class_prefix`
Next we need to generate the gRPC client interfaces from our .proto service definition. We do this using the protocol buffer compiler (`protoc`) with a special gRPC Objective-C plugin.
Next we need to generate the gRPC client interfaces from our .proto service definition. We do this
using the protocol buffer compiler (`protoc`) with a special gRPC Objective-C plugin.
For simplicity, we've provided a [Podspec file](https://github.com/grpc/grpc-common/blob/master/objective-c/route_guide/RouteGuide.podspec) that runs `protoc` for you with the appropriate plugin, input, and output, and describes how to compile the generated files. You just need to run in this directory (`grpc-common/objective-c/route_guide`):
For simplicity, we've provided a [Podspec file](https://github.com/grpc/grpc-common/blob/master/objective-c/route_guide/RouteGuide.podspec)
that runs `protoc` for you with the appropriate plugin, input, and output, and describes how to
compile the generated files. You just need to run in this directory (`grpc-common/objective-c/route_guide`):
```shell
$ pod install
@ -154,20 +198,28 @@ Running this command generates the following files under `Pods/RouteGuide/`:
- `RouteGuide.pbrpc.m`, which contains the implementation of your service classes.
These contain:
- All the protocol buffer code to populate, serialize, and retrieve our request and response message types.
- A class called `RTGRouteGuide` that lets clients call the methods defined in the `RouteGuide` service.
- All the protocol buffer code to populate, serialize, and retrieve our request and response message
types.
- A class called `RTGRouteGuide` that lets clients call the methods defined in the `RouteGuide`
service.
You can also use the provided Podspec file to generate client code from any other proto service definition; just replace the name (matching the file name), version, and other metadata.
You can also use the provided Podspec file to generate client code from any other proto service
definition; just replace the name (matching the file name), version, and other metadata.
<aname="client"></a>
## Creating the client
In this section, we'll look at creating an Objective-C client for our `RouteGuide` service. You can see our complete example client code in [grpc-common/objective-c/route_guide/ViewControllers.m](https://github.com/grpc/grpc-common/blob/master/objective-c/route_guide/ViewControllers.m). (Note: In your apps, for maintainability and readability reasons, you shouldn't put all of your view controllers in a single file; it's done here only to simplify the learning process).
In this section, we'll look at creating an Objective-C client for our `RouteGuide` service. You can
see our complete example client code in [grpc-common/objective-c/route_guide/ViewControllers.m](https://github.com/grpc/grpc-common/blob/master/objective-c/route_guide/ViewControllers.m).
(Note: In your apps, for maintainability and readability reasons, you shouldn't put all of your view
controllers in a single file; it's done here only to simplify the learning process).
### Constructing a client object
To call service methods, we first need to create a client object, an instance of the generated `RTGRouteGuide` class. The designated initializer of the class expects a `NSString *` with the server address and port we want to connect to:
To call service methods, we first need to create a client object, an instance of the generated
`RTGRouteGuide` class. The designated initializer of the class expects a `NSString *` with the
Notice that we've specified the HTTP scheme in the host address. This is because the server we will be using to test our client doesn't use [TLS](http://en.wikipedia.org/wiki/Transport_Layer_Security). This is fine because it will be running locally on our development machine. The most common case, though, is connecting with a gRPC server on the internet, running gRPC over TLS. For that case, the HTTPS scheme can be specified (or no scheme at all, as HTTPS is the default value). The default value of the port is that of the scheme selected: 443 for HTTPS and 80 for HTTP.
Notice that we've specified the HTTP scheme in the host address. This is because the server we will
be using to test our client doesn't use [TLS](http://en.wikipedia.org/wiki/Transport_Layer_Security).
This is fine because it will be running locally on our development machine. The most common case,
though, is connecting with a gRPC server on the internet, running gRPC over TLS. For that case, the
HTTPS scheme can be specified (or no scheme at all, as HTTPS is the default value). The default
value of the port is that of the scheme selected: 443 for HTTPS and 80 for HTTP.
### Calling service methods
Now let's look at how we call our service methods. As you will see, all these methods are asynchronous, so you can call them from the main thread of your app without worrying about freezing your UI or the OS killing your app.
Now let's look at how we call our service methods. As you will see, all these methods are
asynchronous, so you can call them from the main thread of your app without worrying about freezing
your UI or the OS killing your app.
#### Simple RPC
Calling the simple RPC `GetFeature` is nearly as straightforward as calling any other asynchronous method on Cocoa.
Calling the simple RPC `GetFeature` is nearly as straightforward as calling any other asynchronous
method on Cocoa.
```objective-c
RTGPoint *point = [RTGPoint message];
@ -204,7 +264,12 @@ point.longitude = -74E7;
}];
```
As you can see, we create and populate a request protocol buffer object (in our case `RTGPoint`). Then, we call the method on the client object, passing it the request, and a block to handle the response (or any RPC error). If the RPC finishes successfully, the handler block is called with a `nil` error argument, and we can read the response information from the server from the response argument. If, instead, some RPC error happens, the handler block is called with a `nil` response argument, and we can read the details of the problem from the error argument.
As you can see, we create and populate a request protocol buffer object (in our case `RTGPoint`).
Then, we call the method on the client object, passing it the request, and a block to handle the
response (or any RPC error). If the RPC finishes successfully, the handler block is called with a
`nil` error argument, and we can read the response information from the server from the response
argument. If, instead, some RPC error happens, the handler block is called with a `nil` response
argument, and we can read the details of the problem from the error argument.
```objective-c
NSLog(@"Found feature called %@ at %@.", response.name, response.location);
@ -212,10 +277,12 @@ NSLog(@"Found feature called %@ at %@.", response.name, response.location);
#### Streaming RPCs
Now let's look at our streaming methods. Here's where we call the response-streaming method `ListFeatures`, which results in our client receiving a stream of geographical `RTGFeature`s:
Now let's look at our streaming methods. Here's where we call the response-streaming method
`ListFeatures`, which results in our client receiving a stream of geographical `RTGFeature`s:
@ -227,13 +294,18 @@ Now let's look at our streaming methods. Here's where we call the response-strea
}];
```
Notice how the signature of the handler block now includes a `BOOL done` parameter. The handler block can be called any number of times; only on the last call is the `done` argument value set to `YES`. If an error occurs, the RPC finishes and the handler is called with the arguments `(YES, nil, error)`.
Notice how the signature of the `eventHandler` block now includes a `BOOL done` parameter. The
`eventHandler` block can be called any number of times; only on the last call is the `done` argument
value set to `YES`. If an error occurs, the RPC finishes and the block is called with the arguments
`(YES, nil, error)`.
The request-streaming method `RecordRoute` expects a stream of `RTGPoint`s from the cient. This stream is passed to the method as an object that conforms to the `GRXWriter` protocol. The simplest way to create one is to initialize one from a `NSArray` object:
The request-streaming method `RecordRoute` expects a stream of `RTGPoint`s from the cient. This
stream is passed to the method as an object of class `GRXWriter`. The simplest way to create one is
The `GRXWriter` protocol is generic enough to allow for asynchronous streams, streams of future values, or even infinite streams.
The `GRXWriter` class is generic enough to allow for asynchronous streams, streams of future values,
or even infinite streams.
Finally, let's look at our bidirectional streaming RPC `RouteChat()`. The way to call a bidirectional streaming RPC is just a combination of how to call request-streaming RPCs and response-streaming RPCs.
Finally, let's look at our bidirectional streaming RPC `RouteChat()`. The way to call a
bidirectional streaming RPC is just a combination of how to call request-streaming RPCs and
NSLog(@"Got message %@ at %@", note.message, note.location);
} else if (error) {
@ -277,4 +354,7 @@ Finally, let's look at our bidirectional streaming RPC `RouteChat()`. The way to
}];
```
The semantics for the handler block and the `GRXWriter` argument here are exactly the same as for our request-streaming and response-streaming methods. Although both client and server will always get the other's messages in the order they were written, the two streams operate completely independently.
The semantics for the handler block and the `GRXWriter` argument here are exactly the same as for
our request-streaming and response-streaming methods. Although both client and server will always
get the other's messages in the order they were written, the two streams operate completely