Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grpc

Package Overview
Dependencies
Maintainers
3
Versions
138
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grpc

gRPC Library for Node

  • 1.24.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
136K
decreased by-6.99%
Maintainers
3
Weekly downloads
 
Created

What is grpc?

The grpc npm package is a Node.js library for making and handling remote procedure calls (RPC) using the gRPC framework. It allows for efficient communication between services, supports multiple programming languages, and provides features like load balancing, authentication, and more.

What are grpc's main functionalities?

Creating a gRPC Server

This code demonstrates how to create a basic gRPC server in Node.js. It loads a .proto file, defines a service, and starts the server to listen for incoming requests.

const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('path/to/your/protofile.proto', {});
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
const yourService = protoDescriptor.yourService;

function yourFunction(call, callback) {
  callback(null, { message: 'Hello ' + call.request.name });
}

const server = new grpc.Server();
server.addService(yourService.service, { yourFunction: yourFunction });
server.bind('127.0.0.1:50051', grpc.ServerCredentials.createInsecure());
server.start();

Creating a gRPC Client

This code demonstrates how to create a gRPC client in Node.js. It connects to a gRPC server, makes a request, and handles the response.

const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('path/to/your/protofile.proto', {});
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
const yourService = protoDescriptor.yourService;

const client = new yourService('localhost:50051', grpc.credentials.createInsecure());

client.yourFunction({ name: 'World' }, (error, response) => {
  if (!error) {
    console.log('Greeting:', response.message);
  } else {
    console.error(error);
  }
});

Streaming RPC

This code demonstrates how to implement a streaming RPC in a gRPC server. It handles incoming stream data and sends responses back to the client.

const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('path/to/your/protofile.proto', {});
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
const yourService = protoDescriptor.yourService;

function yourStreamFunction(call) {
  call.on('data', (request) => {
    console.log('Received:', request);
    call.write({ message: 'Hello ' + request.name });
  });
  call.on('end', () => {
    call.end();
  });
}

const server = new grpc.Server();
server.addService(yourService.service, { yourStreamFunction: yourStreamFunction });
server.bind('127.0.0.1:50051', grpc.ServerCredentials.createInsecure());
server.start();

Other packages similar to grpc

FAQs

Package last updated on 23 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc