GRPCgRPC for Swift.
grpc-swift is a Swift package that contains a gRPC Swift API and code generator.
It is intended for use with Apple's SwiftProtobuf support for
Protocol Buffers. Both projects contain code generation plugins for protoc,
Google's Protocol Buffer compiler, and both contain libraries of supporting code
that is needed to build and run the generated code.
APIs and generated code is provided for both gRPC clients and servers, and can be built either with Xcode or the Swift Package Manager. Support is provided for all four gRPC API styles (Unary, Server Streaming, Client Streaming, and Bidirectional Streaming) and connections can be made either over secure (TLS) or insecure channels.
gRPC Swift's platform support is identical to the platform support of Swift NIO.
The earliest supported version of Swift for gRPC Swift releases are as follows:
| gRPC Swift Version | Earliest Swift Version |
|---|---|
1.0.0 ..< 1.8.0 |
5.2 |
1.8.0 ..< 1.11.0 |
5.4 |
1.11.0..< 1.16.0. |
5.5 |
1.16.0..< 1.20.0 |
5.6 |
1.20.0..< 1.22.0 |
5.7 |
1.22.0..< 1.24.0 |
5.8 |
1.24.0... |
5.9 |
Versions of clients and services which are use Swift's Concurrency support are available from gRPC Swift 1.8.0 and require Swift 5.6 and newer.
There are two parts to gRPC Swift: the gRPC library and an API code generator.
The Swift Package Manager is the preferred way to get gRPC Swift. Simply add the
package dependency to your Package.swift:
dependencies: [
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.15.0")
]
...and depend on "GRPC" in the necessary targets:
.target(
name: ...,
dependencies: [.product(name: "GRPC", package: "grpc-swift")]
]
Binary releases of protoc, the Protocol Buffer Compiler, are available on
GitHub.
To build the plugins, run the following in the main directory:
$ swift build --product protoc-gen-swift
$ swift build --product protoc-gen-grpc-swift
This uses the Swift Package Manager to build both of the necessary plugins:
protoc-gen-swift, which generates Protocol Buffer support code and
protoc-gen-grpc-swift, which generates gRPC interface code.
To install these plugins, just copy the two executables (protoc-gen-swift and
protoc-gen-grpc-swift) that show up in the main directory into a directory
that is part of your PATH environment variable. Alternatively the full path to
the plugins can be specified when using protoc.
The plugins are available from homebrew and can be installed with:
$ brew install swift-protobuf grpc-swift
gRPC Swift has a number of tutorials and examples available. The
Examples/v1 directory contains examples which do not require
additional dependencies and may be built using the Swift Package Manager.
Some of the examples are accompanied by tutorials, including:
protoc plugin in docs/plugin.mddocs/tls.mddocs/keepalive.mddocs/apple-platforms.md