Skip to content

Try gRPC Swift in Google Cloud Shell

Tim Burks edited this page Dec 30, 2017 · 14 revisions

Google Cloud Shell is a free and easy-to-use Linux shell that is available with every Google Cloud developer account. You can sign up for a free Google Cloud developer account here.

Note! Google Cloud Shell runs in a temporary virtual machine that shuts down when you sign out or are inactive. To get the hosting power of a full-time virtual machine, try gRPC-Swift in a Google Compute Engine VM.

Set up your shell environment.

Open the Google Cloud Shell.

Visit console.cloud.google.com and click on the small icon in the top bar that contains the console prompt (>_). The Google Cloud Shell should open at the bottom of your browser window (for details about Google Cloud Shell see the online documentation).

Use apt-get to install packages needed to run Swift.

sudo apt-get update
sudo apt-get install -y clang clang-3.8 lldb-3.8 libicu-dev libtool \
                            libcurl4-openssl-dev libbsd-dev build-essential \
                            libssl-dev uuid-dev curl unzip

Create a ~/local directory for some tools that we'll download.

cd
mkdir -p local

Add $HOME/local/bin to your path by adding the following at the end of your .bashrc.

export PATH=.:$HOME/local/bin:$PATH

After you've done the above step, reload .bashrc.

source ~/.bashrc

Download and install Swift in ~/local. Note that your shell is running Ubuntu 14.04.

SWIFT_URL=https://swift.org/builds/swift-4.0.3-release/ubuntu1404/swift-4.0.3-RELEASE/swift-4.0.3-RELEASE-ubuntu14.04.tar.gz
echo $SWIFT_URL
curl -fSsL $SWIFT_URL -o swift.tar.gz 
tar -xzf swift.tar.gz --strip-components=2 --directory=local

Download and install the protocol buffer compiler in ~/local.

PROTOC_URL=https://github.com/google/protobuf/releases/download/v3.4.0/protoc-3.4.0-linux-x86_64.zip
echo $PROTOC_URL
curl -fSsL $PROTOC_URL -o protoc.zip
unzip protoc.zip -d local

Try grpc-swift.

Download the grpc-swift repository.

git clone https://github.com/grpc/grpc-swift

Build the Echo example.

cd grpc-swift/Examples/Echo/PackageManager
make

Start the Echo server.

.build/debug/Echo serve &

Test the server with the Echo client.

.build/debug/Echo get
.build/debug/Echo expand
.build/debug/Echo collect
.build/debug/Echo update

You've done it!

The four services that you've called demonstrate the four API modes of gRPC: Unary, Server Streaming, Client Streaming, and Bidirectional Streaming. See echo.proto for the service definition and main.swift and EchoProvider.swift for client and server implementations, respectively. As a gRPC user, that's all the code that you need to write! Everything else is in support libraries or generated by the Protocol Buffer and gRPC plugins.