Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to implement the server #1

Closed
artemyarulin opened this issue May 13, 2020 · 5 comments
Closed

How to implement the server #1

artemyarulin opened this issue May 13, 2020 · 5 comments
Assignees

Comments

@artemyarulin
Copy link

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?

// src/index.ts
import * as grpc from '@grpc/grpc-js'
import * as services from './proto/echo_service_grpc_pb'

class EchoService implements services.IEchoServiceService {
    // ?
}

var server = new grpc.Server();
server.addService(services.EchoServiceService, new EchoService());
server.bindAsync("127.0.0.1:50051", grpc.ServerCredentials.createInsecure(), (err, port) => {
    if (!err) server.start();
});

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?

@badsyntax
Copy link
Owner

I'll add an example to this repo. Will update this issue when I've done so.

@badsyntax badsyntax self-assigned this May 14, 2020
@artemyarulin
Copy link
Author

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 server.addService expects service as a first parameter but in TypeScript it's defined as export const APIService: IAPIService; and evaluated to undefined in runtime. So fix is importing it from JS file, took it from here https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/master/examples/src/grpcjs/server.ts#L75-L76

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();

@badsyntax
Copy link
Owner

badsyntax commented Oct 8, 2020

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.

@badsyntax
Copy link
Owner

I've added a PR to add server types to ts-protoc-gen: improbable-eng/ts-protoc-gen#247

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

@badsyntax
Copy link
Owner

badsyntax commented Dec 16, 2020

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants