New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to implement the server #1
Comments
I'll add an example to this repo. Will update this issue when I've done so. |
Figured out, had two issues - server interface was indeed missing, in my project that was a solution agreatfool/grpc_tools_node_protoc_ts#38 (comment) Then implementing interface is straightforward with one exception that Full example import * as grpc from "@grpc/grpc-js";
import {sendUnaryData} from "@grpc/grpc-js/build/src/server-call";
import {TestReq, TestResp} from "./proto/schema_pb";
import {IAPIServer} from "./proto/schema_grpc_pb";
import * as schemaGRPC from "./proto/schema_grpc_pb"
class API implements IAPIServer {
public test(call: grpc.ServerUnaryCall<TestReq, TestResp>, callback: sendUnaryData<TestResp>) {
const book = new TestResp();
callback(null, book);
}
}
function startServer() {
const server = new grpc.Server();
// HACK: Service definition is missing in TypeScript file and has to be exported from JS file
// @ts-ignore
server.addService(schemaGRPC['schema.API'], new API());
server.bindAsync("127.0.0.1:50051", grpc.ServerCredentials.createInsecure(), (err, port) => {
if (err) {
throw err;
}
console.log(`Server started, listening: 127.0.0.1:${port}`);
server.start();
});
}
startServer(); |
There's a couple things to note here:
I am attempting to improve the typings here but it requires changes from grpc upstream. See grpc/grpc-node#1590, grpc/grpc-node#1587, grpc/grpc-node#1474 (comment) Once the types have improved in upstream, i will update both https://github.com/improbable-eng/ts-protoc-gen and https://github.com/agreatfool/grpc_tools_node_protoc_ts as well as the examples in this repo. |
I've added a PR to add server types to I'd recommend checking out the soon-to-be-released proto-loader typescript generator: https://github.com/badsyntax/grpc-js-types/tree/master/examples/grpc-proto-loader |
All 3 tools now generate a server interface! Hopefully this will make life easier for people. I've updated the examples in this repo. Am closing now. |
Thank you very much for your example, but I'm not sure how to implement the actual logic and a server?
I guess it should look something like that, but I struggle how to actually implement the interface?
Or is there is something missing? For example in https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/b7b5e97349d3f9463214f4cc65e2ec2c9e44320a/examples/src/grpcjs/proto/book_grpc_pb.d.ts#L57-L62 there is also additional interface
IBookServiceServer
defined which I guess should be used as implementation interface for servers but here it's missing?The text was updated successfully, but these errors were encountered: