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

E1cf27d9fa565b2867d171f21a39759c?s=47 tikidunpon
August 30, 2018

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

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

E1cf27d9fa565b2867d171f21a39759c?s=128

tikidunpon

August 30, 2018
Tweet

Transcript

  1. 9.
  2. 10.
  3. 11.
  4. 12.
  5. 13.
  6. 14.
  7. 19.

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

    Netflix, Square..etc • Cyber Agent, Mercari, 10x, Origami..etc
  8. 33.

    Protocol Buffersʢলུʣ service Greeter { // αʔϏεఆٛ rpc SayHello (HelloRequest)

    returns (HelloResponse) {}ɹ // RPCϝιουఆٛ } message HelloRequest {ɹ// ϦΫΤετϝοηʔδఆٛ string text = 1; // ϑΟʔϧυܕ ϑΟʔϧυ໊ ϑΟʔϧυ൪߸ } message HelloResponse { // Ϩεϙϯεϝοηʔδఆٛ string text = 1; }
  9. 34.

    Protocol Buffers class Greeter_GreeterServiceClient { func sayHello(){} } struct Greeter_HelloRequest

    {ɹ var text: String = String() } struct Greeter_HelloResponse { var text: String = String() }
  10. 35.

    4छྨͷετϦʔϛϯά௨৴ RPC ϦΫΤετ਺ Ϩεϙϯε਺ ར༻ྫ Unary 1 1 ୯७ͳAPIɺϚΠΫ ϩαʔϏεؒ௨৴

    Server streaming 1 ෳ਺ ϑΟʔυ഑৴ɺαʔ όʔPush Client streaming ෳ਺ 1 ෳ਺σʔλૹ৴ Bidirectional streaming ෳ਺ ෳ਺ speech-to-textɺνϟ οτ
  11. 51.

    γϯϓϧͳAPIͷ࣮૷ʢprotoͷఆٛʣ syntax = "proto3"; package greeter; service Greeter { rpc

    SayHello (HelloRequest) returns (HelloResponse) {} } message HelloRequest { string text = 1; } message HelloResponse { string text = 1; }
  12. 54.

    ࣗಈੜ੒ίʔυʢൈਮʣ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) } }
  13. 55.

    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) } }
  14. 56.

    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") } }) }
  15. 57.

    ଞͷετϦʔϛϯά௨৴Λѻ͏ʹ͸ 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) {} }
  16. 58.
  17. 59.
  18. 62.

    Next Step!! • grpc-swiftΛ࢖ͬͯiOSΞϓϦͰ΋շదͳgRPC௨৴Λߦ͏ • Plasma - gRPC streamΛར༻ͨ͠ϦΞϧλΠϜͳϢʔβʔମݧ •

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