Search code examples
javascriptnode.jsgrpc

How to set max_receive_message_length globally?


I am using grpc-dynamic-gateway package and it uses the grpc package as a client. Im trying to figure out how I can set max_receive_message_length globally on the grpc object I require in my script?

const grpc = require('grpc');

grpc.max_receive_message_length = 1024 * 1024 * 100; // <-- this does not work, how can I do this?

app.use('/', grpcGateway(
  ['api.proto'],
  '0.0.0.0:5051',
  grpc.credentials.createInsecure(),
  true,
  process.cwd(),
  grpc // This is being passed to the middleware..id like it to have the option above set on it
));

Solution

  • I figured this out. The change needs to be made directly in the grpc-dynamic-gateway package. In index.js on line 77 change:

    getPkg(clients, pkg, true)[svc] = new (getPkg(protos[si], pkg, false))[svc](grpcLocation, credentials)
    

    to

    getPkg(clients, pkg, true)[svc] = new (getPkg(protos[si], pkg, false))[svc](grpcLocation, credentials, {
         "grpc.max_receive_message_length": 1024 * 1024 * 1000,
         "grpc.max_send_message_length": 1024 * 1024 * 1000
    })
    

    Im not sure if this is the only way to fix this but it worked for me. Also obviously adjust the message sizes to a more appropriate value for yourself. 1000mb is probably excessive.