Skip to content

Instantly share code, notes, and snippets.

@andriitishchenko
Last active April 26, 2024 22:54
Show Gist options
  • Save andriitishchenko/8cc4aea53b7b385adc60966933bb2ee0 to your computer and use it in GitHub Desktop.
Save andriitishchenko/8cc4aea53b7b385adc60966933bb2ee0 to your computer and use it in GitHub Desktop.

Revisions

  1. andriitishchenko revised this gist Apr 26, 2024. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions Swift gRPC.md
    Original file line number Diff line number Diff line change
    @@ -76,8 +76,8 @@ _ = client.sayHello(helloRequest).response.always { result in
    ```

    More examples:
    https://medium.com/@thomsmed/5-tips-when-getting-started-with-grpc-and-ios-9e4323bd6b98
    https://mtanriverdi.medium.com/grpc-call-on-ios-with-swift-ef11d3151cbf
    https://gaitatzis.medium.com/building-a-grpc-client-in-ios-swift-3b0d36999678
    https://medium.com/@hardik.7091/guide-to-integrating-grpc-server-and-swift-client-32dc1a05aa8e
    https://levelup.gitconnected.com/swift-grpc-577ce1a4d1b7
    - https://medium.com/@thomsmed/5-tips-when-getting-started-with-grpc-and-ios-9e4323bd6b98
    - https://mtanriverdi.medium.com/grpc-call-on-ios-with-swift-ef11d3151cbf
    - https://gaitatzis.medium.com/building-a-grpc-client-in-ios-swift-3b0d36999678
    - https://medium.com/@hardik.7091/guide-to-integrating-grpc-server-and-swift-client-32dc1a05aa8e
    - https://levelup.gitconnected.com/swift-grpc-577ce1a4d1b7
  2. andriitishchenko revised this gist Apr 26, 2024. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions Swift gRPC.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    ![alt text](https://miro.medium.com/v2/resize:fit:4800/format:webp/1*ib3Xb4TibYX7RKfA_d7hXA.png)

    # install protoc , protoc-gen-swift , protoc-gen-grpc-swift
    ```
    brew install protobuf swift-protobuf grpc-swift grpcurl
    @@ -73,6 +75,9 @@ _ = client.sayHello(helloRequest).response.always { result in
    }
    ```

    Auth example:

    https://gaitatzis.medium.com/building-a-grpc-client-in-ios-swift-3b0d36999678
    More examples:
    https://medium.com/@thomsmed/5-tips-when-getting-started-with-grpc-and-ios-9e4323bd6b98
    https://mtanriverdi.medium.com/grpc-call-on-ios-with-swift-ef11d3151cbf
    https://gaitatzis.medium.com/building-a-grpc-client-in-ios-swift-3b0d36999678
    https://medium.com/@hardik.7091/guide-to-integrating-grpc-server-and-swift-client-32dc1a05aa8e
    https://levelup.gitconnected.com/swift-grpc-577ce1a4d1b7
  3. andriitishchenko revised this gist Apr 26, 2024. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Swift gRPC.md
    Original file line number Diff line number Diff line change
    @@ -72,3 +72,7 @@ _ = client.sayHello(helloRequest).response.always { result in
    }
    }
    ```

    Auth example:

    https://gaitatzis.medium.com/building-a-grpc-client-in-ios-swift-3b0d36999678
  4. andriitishchenko revised this gist Apr 26, 2024. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions Swift gRPC.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,8 @@ cd grpc && git sparse-checkout set examples
    # Start Python server on a localhost

    ```
    grpc/examples/python/helloworld/greeter_server_with_reflection.py <-- Server we need
    cd grpc/examples/python/helloworld/
    python3 -m pip install grpcio grpcio-tools grpcio-reflection
    python3 greeter_server_with_reflection.py
    @@ -24,23 +26,21 @@ grpcurl -plaintext -d '{"name": "TEST"}' localhost:50051 helloworld.Greeter.Say
    # Swift

    ```
    grpc/examples/protos/helloworld.proto
    grpc/examples/protos/helloworld.proto <-- convert it to Swift
    cd grpc/examples/protos/
    ```
    ```
    protoc --swift_out=. --grpc-swift_out=Client=true,Server=false:. helloworld.proto
    ```
    Import to Xcode files:
    - helloworld.grpc.swift
    - helloworld.pb.swift

    ```
    File->Swift Packages->Add Package Dependency
    Xcode->File->Swift Packages->Add Package Dependency
    https://github.com/grpc/grpc-swift
    ```

    Add GRPC to your target
    Add GRPC to your app target


    # Swift Client example
  5. andriitishchenko revised this gist Apr 26, 2024. 1 changed file with 25 additions and 21 deletions.
    46 changes: 25 additions & 21 deletions Swift gRPC.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # install protoc , protoc-gen-swift , protoc-gen-grpc-swift
    ```
    brew install protobuf swift-protobuf grpc-swift
    brew install protobuf swift-protobuf grpc-swift grpcurl
    ```

    ### Use examples
    @@ -16,6 +16,10 @@ cd grpc/examples/python/helloworld/
    python3 -m pip install grpcio grpcio-tools grpcio-reflection
    python3 greeter_server_with_reflection.py
    ```
    ### Test the local server
    ```
    grpcurl -plaintext -d '{"name": "TEST"}' localhost:50051 helloworld.Greeter.SayHello
    ```

    # Swift

    @@ -47,24 +51,24 @@ import NIO
    ```

    ```
    let conf = ClientConnection.Configuration.default(
    target:.hostAndPort("localhost", 50051),
    eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1))
    let connection: ClientConnection = ClientConnection.init(configuration: conf)
    let helloRequest = Helloworld_HelloRequest.with {
    $0.name = "SwiftUI"
    }
    let client = Helloworld_GreeterNIOClient(channel: connection)
    _ = client.sayHello(helloRequest).response.always { result in
    switch result {
    case let .success(helloResponse):
    print(helloResponse.message)
    case let .failure(error):
    print(error)
    }
    }
    let conf = ClientConnection.Configuration.default(
    target:.hostAndPort("localhost", 50051),
    eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1))
    let connection: ClientConnection = ClientConnection.init(configuration: conf)
    let helloRequest = Helloworld_HelloRequest.with {
    $0.name = "Swift"
    }
    let client = Helloworld_GreeterNIOClient(channel: connection)
    _ = client.sayHello(helloRequest).response.always { result in
    switch result {
    case let .success(helloResponse):
    print(helloResponse.message)
    case let .failure(error):
    print(error)
    }
    }
    ```
  6. andriitishchenko created this gist Apr 26, 2024.
    70 changes: 70 additions & 0 deletions Swift gRPC.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    # install protoc , protoc-gen-swift , protoc-gen-grpc-swift
    ```
    brew install protobuf swift-protobuf grpc-swift
    ```

    ### Use examples
    ```
    git clone --depth 1 --filter=blob:none --sparse https://github.com/grpc/grpc
    cd grpc && git sparse-checkout set examples
    ```

    # Start Python server on a localhost

    ```
    cd grpc/examples/python/helloworld/
    python3 -m pip install grpcio grpcio-tools grpcio-reflection
    python3 greeter_server_with_reflection.py
    ```

    # Swift

    ```
    grpc/examples/protos/helloworld.proto
    cd grpc/examples/protos/
    ```
    ```
    protoc --swift_out=. --grpc-swift_out=Client=true,Server=false:. helloworld.proto
    ```
    Import to Xcode files:
    - helloworld.grpc.swift
    - helloworld.pb.swift

    ```
    File->Swift Packages->Add Package Dependency
    https://github.com/grpc/grpc-swift
    ```

    Add GRPC to your target


    # Swift Client example

    ```
    import GRPC
    import NIO
    ```

    ```
    let conf = ClientConnection.Configuration.default(
    target:.hostAndPort("localhost", 50051),
    eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1))
    let connection: ClientConnection = ClientConnection.init(configuration: conf)
    let helloRequest = Helloworld_HelloRequest.with {
    $0.name = "SwiftUI"
    }
    let client = Helloworld_GreeterNIOClient(channel: connection)
    _ = client.sayHello(helloRequest).response.always { result in
    switch result {
    case let .success(helloResponse):
    print(helloResponse.message)
    case let .failure(error):
    print(error)
    }
    }
    ```