Using the response from bidirectional streaming callback in Node.js gRPC client

1k views Asked by At

I have a grpc server that is written in Go and runs on a unix domain socket. I'm writing a nodeJS client for for this. Since grpc-node doesn't support unix sockets, i had to use @grpc/grpc-js.

Now the issue i'm facing is that I need to perform some operation on the response that i get back from the server. I currently have this and it works. But is there a cleaner/nicer way of accomplishing this?

    let appEncryptionDef = protoLoader.loadSync(__dirname + '../../../../protos/appencryption.proto', {
        keepCase: true,
        defaults: true,
        oneofs: true
    });
    let appEncryptionProto = grpc.loadPackageDefinition(appEncryptionDef);
    let client = new appEncryptionProto.asherah.apps.server.AppEncryption(`unix://${socket}`, grpc.credentials.createInsecure());
    let call = client.session();

    call.on('data', function (sessionResponse) {
        switch (sessionResponse.response) {
            case 'x':
                data = parse_server_response;
                call.write(data);
                call.end();

                break;
            case 'error_response':
                console.log('error received: ' + sessionResponse.error_response.message);

                break;
        }
    });

Is there a better way of doing this? I've looked at the grpc-caller library but using that with @grpc/grpc-js gives me a Channel's second argument must be a ChannelCredentials error.

Here's the client I've written so far: https://github.com/godaddy/asherah/blob/servicelayer_node/server/samples/clients/node/appencryption_client.js

0

There are 0 answers