This repository contains an experimental Swift gRPC API and code generator.
It is intended for use with Apple's
support for Protocol Buffers. Both projects contain
code generation plugins for
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.
The Echo example provides a comprehensive demonstration of currently-supported features.
Swift gRPC is currently available from CocoaPods.
To integrate, add the following line to your
pod install from command line and use your project's generated
When not using CocoaPods, Swift gRPC includes vendored copies of the gRPC Core library and BoringSSL (an OpenSSL fork that is used by the gRPC Core). These are built automatically in Swift Package Manager builds.
After building your project, add the generated
SwiftGRPC.xcodeproj to your project, and add build dependencies
on BoringSSL, CgRPC, and SwiftGRPC.
See Echo for a working Xcode-based example, and don't hesitate to file issues if you find any problems.
The recommended way to use Swift gRPC is to first define an API using the Protocol Buffer language and then use the Protocol Buffer Compiler and the Swift Protobuf and Swift gRPC plugins to generate the necessary support code.
Getting the plugins
Binary releases of
protoc, the Protocol Buffer Compiler, are
available on GitHub.
To build the plugins, run
make plugin in the main directory.
This uses the Swift Package Manager to build both of the necessary
protoc-gen-swift, which generates Protocol Buffer support code
protoc-gen-swiftgrpc, which generates gRPC interface code.
To install these plugins, just copy the two executables (
protoc-gen-swiftgrpc) that show up in the main directory into a directory that is part of your
PATH environment variable.
Using the plugins
To use the plugins,
protoc and both plugins should be in your
search path (see above). Invoke them with commands like the following:
protoc <your proto files> \ --swift_out=. \ --swiftgrpc_out=.
By convention the
--swift_out option invokes the
To pass extra parameters to the plugin, use a comma-separated parameter list separated from the output directory by a colon.
||ACL of generated code|
||Whether to generate server code|
||Whether to generate client code|
||Whether to generate asynchronous code|
||Whether to generate synchronous code|
||Whether to generate test stub code|
$ protoc <your proto> --swiftgrpc_out=Client=true,Server=false:.
Building your project
grpc-swift development is done with the Swift Package Manager.
For usage in Xcode projects, we rely on the
swift package generate-xcodeproj
command to generate an Xcode project for the
grpc-swift core libraries.
The top-level Makefile uses the Swift Package Manager to generate an Xcode project for the SwiftGRPC package:
$ make && make project
This will create
SwiftGRPC.xcodeproj, which you should
add to your project, along with setting the necessary build dependencies
While the recommended way to use gRPC is with Protocol Buffers and generated code, at its core gRPC is a powerful HTTP/2-based communication system that can support arbitrary payloads. As such, each gRPC library includes low-level interfaces that can be used to directly build API clients and servers with no generated code. For an example of this in Swift, please see the Simple example.
Having build problems?
grpc-swift depends on Swift, Xcode, and swift-protobuf. We are currently testing with the following versions:
- Xcode 9.1
- Swift 4.0
- swift-protobuf 1.1.1
SwiftGRPCNIO is a clean-room implementation of the gRPC protocol on top of the
SwiftNIO library. This implementation is not yet production-ready as it lacks several things recommended for production use:
- Better test coverage
- Full error handling
- SSL support
- Client support
- Example projects
- iOS support
- Removal of the
However, if you are planning to implement a gRPC service based on
SwiftNIO or the Vapor framework, you might find this package useful. In addition, once ready, this package should provide more predictable and reliable behavior in the future, combined with an improved API and better developer experience.
You may also want to have a look at this presentation for more details on the motivation for this package.
Please get involved! See our guidelines for contributing.
Prior to creating a new release tag for SwiftGRPC, the
.podspec file's version should be bumped, and the
CocoaPods spec linter should be run to ensure that there are no new warnings/errors:
$ pod spec lint SwiftGRPC.podspec
Once a new release tag is created, the updated CocoaPods spec should also be pushed to the master specs repo:
$ pod trunk push SwiftGRPC.podspec