How to debug Swift gRPC client

277 views Asked by At

I'm using gRPCs to communicate between the server and the client. I have a nodeJS server and a Swift client.

I used this article to build my Swift client but I'm unable to talk to my nodeJS server on localhost. I was wondering if someone had ways to debug this problem.

I already checked the nodeJS server I created on Postman so that should work properly. I pasted the code below to see if I'm clearly missing something. Thank you!

    func prepareGrpc() {
        let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)
        
        defer {
          try? group.syncShutdownGracefully()
        }

        do {
            let channel = try GRPCChannelPool.with(
                target: .host("localhost", port: 50051),
                transportSecurity: .plaintext,
                eventLoopGroup: group
            )
            
            client = BookStorePackage_BookAsyncClient(channel: channel)
        } catch {
            print("Can't prepare grpc")
        }
    }
    
    func createBook(id: Int32, bookName: String) async throws {
        do {
            let book: BookStorePackage_BookItem = .with {
                $0.id = id
                $0.book = bookName
            }
            
            let feature = try await client?.createBook(book)
            
            guard let created_book = feature else {
                print("Failed creating book")
                return
            }
            
            print("Created book with id: \(created_book.id), name: \(created_book.book)")
        } catch {
            print("Error in creating a book")
        }
    }

Update: This works (with error below) but I'm not sure if I should be opening and closing the connection like this or keep it open for the future

Error:

2023-07-02 13:09:00.936066-0700 bookStore[81602:30021510] [connection] nw_socket_handle_socket_event [C1.1.1:1] Socket SO_ERROR [61: Connection refused]
func createBook(id: Int32, bookName: String) async throws {
        let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)

        // Make sure the group is shutdown when we're done with it.
        defer {
          try! group.syncShutdownGracefully()
        }

        // Configure the channel, we're not using TLS so the connection is `insecure`.
        let channel = try GRPCChannelPool.with(
          target: .host("localhost", port: 50051),
          transportSecurity: .plaintext,
          eventLoopGroup: group
        )

        defer {
          print("Closing the connection")
          try! channel.close().wait()
        }

        let bookClient = BookStorePackage_BookAsyncClient(channel: channel)

        let book: BookStorePackage_BookItem = .with {
            $0.id = id
            $0.book = bookName
        }

        do {
          let bookResponse = try await bookClient.createBook(book)
          print("Book response received: \(bookResponse.book) with \(bookResponse.id)")
        } catch {
          print("Book response failed: \(error)")
        }
    }

0

There are 0 answers