Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOSエンジニアの為のgrpc-swift入門

tanako
August 30, 2018

 iOSエンジニアの為のgrpc-swift入門

gRPCはGoogleが開発したハイパフォーマンスなRPC(Remote Procedure Call)プロトコルです。
Firebase SDKにも組み込まれていて、間接的に使った方も多いと思います。
トークでは、アプリからの通信の選択肢として、RESTやGraphQLに加えてgRPCも選べるように、
gRPC自体の解説、grpc-swiftの開発状況や現状の課題をデモを交えお伝えします。

tanako

August 30, 2018
Tweet

More Decks by tanako

Other Decks in Technology

Transcript

  1. gRPC࠾༻ࣄྫ • Firebase Firestore • Google Cloud Speech • Lyft,

    Netflix, Square..etc • Cyber Agent, Mercari, 10x, Origami..etc
  2. Protocol Buffersʢলུʣ service Greeter { // αʔϏεఆٛ rpc SayHello (HelloRequest)

    returns (HelloResponse) {}ɹ // RPCϝιουఆٛ } message HelloRequest {ɹ// ϦΫΤετϝοηʔδఆٛ string text = 1; // ϑΟʔϧυܕ ϑΟʔϧυ໊ ϑΟʔϧυ൪߸ } message HelloResponse { // Ϩεϙϯεϝοηʔδఆٛ string text = 1; }
  3. Protocol Buffers class Greeter_GreeterServiceClient { func sayHello(){} } struct Greeter_HelloRequest

    {ɹ var text: String = String() } struct Greeter_HelloResponse { var text: String = String() }
  4. 4छྨͷετϦʔϛϯά௨৴ RPC ϦΫΤετ਺ Ϩεϙϯε਺ ར༻ྫ Unary 1 1 ୯७ͳAPIɺϚΠΫ ϩαʔϏεؒ௨৴

    Server streaming 1 ෳ਺ ϑΟʔυ഑৴ɺαʔ όʔPush Client streaming ෳ਺ 1 ෳ਺σʔλૹ৴ Bidirectional streaming ෳ਺ ෳ਺ speech-to-textɺνϟ οτ
  5. γϯϓϧͳAPIͷ࣮૷ʢprotoͷఆٛʣ syntax = "proto3"; package greeter; service Greeter { rpc

    SayHello (HelloRequest) returns (HelloResponse) {} } message HelloRequest { string text = 1; } message HelloResponse { string text = 1; }
  6. ࣗಈੜ੒ίʔυʢൈਮʣhelloworld.grpc.swift internal final class Greeter_GreeterServiceClient: ServiceClientBase, Greeter_GreeterService { /// Synchronous.

    Unary. internal func sayHello(_ request: Greeter_HelloRequest) throws -> Greeter_HelloResponse { return try Greeter_GreeterSayHelloCallBase(channel) .run(request: request, metadata: metadata) } /// Asynchronous. Unary. internal func sayHello(_ request: Greeter_HelloRequest, completion: @escaping (Greeter_HelloResponse?, CallResult) -> Void) throws -> Greeter_GreeterSayHelloCall { return try Greeter_GreeterSayHelloCallBase(channel) .start(request: request, metadata: metadata, completion: completion) } }
  7. gRPCΫϥΠΞϯτʢUnary Blocking) let client = Greeter_GreeterServiceClient.init(address: "127.0.0.1:50051", secure: false) class

    ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() blockingSayHello() } func blockingSayHello() { var request = Greeter_HelloRequest() request.text = "hello" let response = try? self.client.sayHello(request) print("gRPC Server returns " + response!.text) } }
  8. gRPCΫϥΠΞϯτʢUnary Non Blocking) // ඇಉظॲཧ func nonBlockingSayHello() { var request

    = Greeter_HelloRequest() request.text = "hello" let _ = try? self.client.sayHello(request, completion: { (response, result) in if result.success, result.statusCode == .ok { print("gRPC Server returns " + response!.text) } else { print("gRPC Server returns error") } }) }
  9. ଞͷετϦʔϛϯά௨৴Λѻ͏ʹ͸ service Echo { //Unary rpc Get(EchoRequest) returns (EchoResponse) {}

    //Server streaming rpc Expand(EchoRequest) returns (stream EchoResponse) {} //Client streaming rpc Collect(stream EchoRequest) returns (EchoResponse) {} //Bidirectional streaming rpc Update(stream EchoRequest) returns (stream EchoResponse) {} }
  10. Next Step!! • grpc-swiftΛ࢖ͬͯiOSΞϓϦͰ΋շదͳgRPC௨৴Λߦ͏ • Plasma - gRPC streamΛར༻ͨ͠ϦΞϧλΠϜͳϢʔβʔମݧ •

    grpc.io: Official Site • grpc-swift: Github • swift-protobuf: Github • gRPC & iOS at Lyft: YouTube