Code Monkey home page Code Monkey logo

Comments (9)

murgatroid99 avatar murgatroid99 commented on June 13, 2024

As stated in the release notes, detailed information about that feature can be found in gRFC L112. All of the information you need can be found there. If you have specific questions feel free to follow up.

from grpc-node.

kirankumarkn avatar kirankumarkn commented on June 13, 2024

Is there any document, I can referrer for configuring in Js , not typescript, please let me know Thanks

Note: interceptors will only be called when a request to a registered method is made.
This note says we have to register a request , so does this support JavaScript. If so Can you please add an example for configuration

from grpc-node.

murgatroid99 avatar murgatroid99 commented on June 13, 2024

The API for JavaScript is the same as for TypeScript, except that you don't use any types.

This example demonstrates registering a service, specifically on this line

from grpc-node.

kirankumarkn avatar kirankumarkn commented on June 13, 2024

I have followed adding a server interceptor in the below code. But when you call service from client , console log in server interceptor it is not logged. Please let me know how to configure listener , where is start method in responder to configure

// Server code
const listener = (new grpc.ServerListenerBuilder())
  .withOnReceiveMetadata(function (metadata, next) {
    logger.log(metadata);
    next(metadata);
  })
  .withOnReceiveMessage(function (message, next) {
    logger.log(message);
    next(message);
  })
  .build();

const serverInterceptor  = {
    start :(next) => {
      next(listener)
    }
  }

const requester = (new grpc.ResponderBuilder())
  .withSendMetadata(function (metadata, next) {
    logger.log(metadata);
    next(metadata);
  })
  .withSendMessage(function (message, next) {
    logger.log(message);
    next(message);
  })
  .build();


const interceptors = function interceptor(methodDescriptor, call) {
  return new grpc.ServerInterceptingCall(serverInterceptor, requester);
}

const server = new grpc.Server([interceptors])

server.addService(linksProto.LinkService.service, {
  ListLinks: listLinks
})

server.bindAsync(
  "localhost:30043",
  grpc.ServerCredentials.createInsecure(), () => {
    server.start()
    console.log(`Server running at ${serverAddress}`)
  })
// Client code 

function main() {
    let metadata = new grpc.Metadata();
    metadata.add('authorization', 'test Data')
    
    let client = new LinkService("localhost:30043", grpc.credentials.createInsecure());
    client.ListLinks({"parent":"partners/partner-1"},metadata,(err,data) => {
      if(!err){
        console.log('data', JSON.stringify(data));
      }
      else{
        console.log('error', err);
      }
    });
}

from grpc-node.

kirankumarkn avatar kirankumarkn commented on June 13, 2024

I think need a document on how to configure serverListener. Please check above server code and client code, when I call service interceptors are not called. Please let me know if any suggestions

from grpc-node.

murgatroid99 avatar murgatroid99 commented on June 13, 2024

Your server constructor call should be

const server = new grpc.Server({interceptors: [interceptors]});

from grpc-node.

kirankumarkn avatar kirankumarkn commented on June 13, 2024

Your server constructor call should be

const server = new grpc.Server({interceptors: [interceptors]});

Thank you for pointing out the issue, I am able to configure interceptors and log metadata and message in listener.

I have one query regarding handling metadata, usually metadata is handled for validating tokens, if the token is invalid. how do you handle , I don't see the call back function to throw an error. if I throw an error inside as below code. server is getting aborted here. please let me know how to handle the scenarios to stop further and throw back error to client

ex : withOnReceiveMetadata(function (metadata, next) {
console.log('meta data Received', metadata);
if(!token){
throw new Error('Invalid token authentication failed')
}
next(metadata);
})

from grpc-node.

murgatroid99 avatar murgatroid99 commented on June 13, 2024

To do that, you would need to interact with the call passed in to the interceptor function, and call call.sendStatus. Here is an example in the tests.

from grpc-node.

kirankumarkn avatar kirankumarkn commented on June 13, 2024

Thank you @murgatroid99.

from grpc-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.