https://github.com/zackify/swift-web-grpc-example
Example Swift GRPC Server and web client
https://github.com/zackify/swift-web-grpc-example
Last synced: about 2 months ago
JSON representation
Example Swift GRPC Server and web client
- Host: GitHub
- URL: https://github.com/zackify/swift-web-grpc-example
- Owner: zackify
- Created: 2020-03-06T20:42:18.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T09:17:28.000Z (about 3 years ago)
- Last Synced: 2025-02-08T03:47:24.475Z (about 1 year ago)
- Language: Swift
- Size: 4.01 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## GRPC Example
This is a little grpc example using a swift grpc server, and a basic js web client.
Running the code is the easy part, inside the swift folder, `swift run Server`.
Inside web, `yarn` `yarn start`
## Gotchas
Along the way I learned that generating code from protobufs is a bit finicky in swift and JS. Most of my issues were inside swift though, because the grpc-swift package is currently undergoing a rewrite, you have to pull down the beta branch, manually build the swift protobuf and grpc binaries. THEN you can correctly generate swift code from a protobuf.
On the JS side, it was easier, download their listed binary, and run the command. But I find the process a bit annoying. Having to manually download a binary when theres an update needs to be automated on our machines... bazel? ;)
## Swift
On the swift side, I mostly copied the example code, but learned a lot by manually generating the code and reading through it thoroughly. I find the swift code less verbose than JS, but could still be a little shorter. Here's an example request:
```swift
class GreeterProvider: Helloworld_GreeterProvider {
func sayHello(
request: Helloworld_HelloRequest,
context: StatusOnlyCallContext
) -> EventLoopFuture {
let recipient = request.name.isEmpty ? "stranger" : request.name
let response = Helloworld_HelloReply.with {
$0.message = "Hello \(recipient)!"
}
return context.eventLoop.makeSucceededFuture(response)
}
}
```
For the most part it's "just a function" with the request data passed in. These types are all generated for you from the protobuf generator for swift.
## JS
Also, the commands are really crazy looking. Here's how i generated the required JS code for a basic protobuf:
```
protoc -I=. helloworld.proto \
--js_out=import_style=commonjs:. \
--grpc-web_out=import_style=typescript,mode=grpcwebtext:.
```
Lastly, I find the JS synax to be very verbose, and it also doesnt support async/await currently:
```js
let client = new GreeterClient('http://localhost:51318');
let request = new HelloRequest();
request.setName('Test name');
client.sayHello(request, null, (err, res) => {
if (err) return console.log(err);
console.log('got stuff from grpc', res.getMessage());
});
```
I would have expected something much cleaner, like:
```js
let client = new GreeterClient('http://localhost:51318');
try {
let response = await HelloRequest({ name: 'Test name' });
console.log('got stuff from grpc', res.getMessage());
} catch (e) {
console.log(e);
}
```