0 purchases
better grpc generator
better_grpc_generator #
Install
Usage
Why this exists?
A simple way to use the server implementation of grpc as a offline one as well!
It takes the generated files from protoc compiler and does the code generation from it.
So, you will have to have protoc installed to use this tool.
Install #
install protoc
Currently only tested on 3.12.4 but should work on greater versions.
compile grpc from .proto files
You can look at Regenerate gRPC code
or use do like that:
protoc --dart_out=grpc:lib/src/generated -Iprotos protos/helloworld.proto
copied to clipboard
add it better_grpc_generator to your pubspec.yaml
dev_dependencies:
better_grpc_generator: ^0.0.1
build_runner: ^2.1.11
copied to clipboard
get it
dart pub get
copied to clipboard
Usage #
generate the code!
dart run build_runner build --delete-conflicting-outputs
copied to clipboard
Using the generated code #
This tool generate classes with the prefix Better so, let's say your service name is Hello for example, normally you'd use HelloServiceBase but here you use BetterHelloServiceBase and implement the actual functionality for your server, like this:
class HelloService extends BetterHelloServiceBase {
...
}
copied to clipboard
This isn't a service by itself, to make a service just combine it with BetterHelloService.
final service = BetterHelloService(HelloService());
copied to clipboard
Now you can use this service as you'd usually do, but to make things more easier.
Create a server #
final server = Server([
BetterHelloService(HelloService()),
]);
await server.serve(port: port);
print('Server listening on port ${server.port}...');
copied to clipboard
Connect to the server #
final channel = ClientChannel(
'localhost',
port: port,
options: ChannelOptions(
credentials: ChannelCredentials.insecure(),
),
);
final client = BetterHelloClient(HelloClient(channel));
copied to clipboard
Create offline server #
Here the word Client doesn't mean that it connectes to a server. It's just for reference that this class is extendsed from BetterHelloClientBase.
final offlineClient = BetterHelloOfflineClient(HelloService());
copied to clipboard
Why this exists? #
You ask what the benefit of doing this?
Well.. doing this will save you time when developing applications that need to use offline and online connections using the same logic.
Here all the offline and the online clients are inherited from the same class so that it makes it more easy to deal with.
Other notes #
The tool parses the generated files from protoc that only have the extension .pbgrpc.dart.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.