Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
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.
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();
The grpc-web package allows gRPC to be used in web applications. It provides a JavaScript client library that can communicate with gRPC services from the browser. This package is useful for integrating gRPC with frontend applications, whereas the grpc package is primarily focused on server-side and backend communication.
Alpha : Ready for early adopters
This requires node
to be installed. If you instead have the nodejs
executable on Debian, you should install the nodejs-legacy
package.
First, clone this repository (NPM package coming soon). Then follow the instructions in the INSTALL
file in the root of the repository to install the C core library that this package depends on.
Then, simply run npm install
in or referencing this directory.
To run the test suite, simply run npm test
in the install location.
This library internally uses ProtoBuf.js, and some structures it exports match those exported by that library
If you require this module, you will get an object with the following members
function load(filename)
Takes a filename of a Protocol Buffer file, and returns an object representing the structure of the protocol buffer in the following way:
service
member that can be used for constructing servers.function loadObject(reflectionObject)
Returns the same structure that load
returns, but takes a reflection object from ProtoBuf.js
instead of a file name.
function buildServer(serviceArray)
Takes an array of service objects and returns a constructor for a server that handles requests to all of those services.
status
An object mapping status names to status code numbers.
callError
An object mapping call error names to codes. This is primarily useful for tracking down certain kinds of internal errors.
Credentials
An object with factory methods for creating credential objects for clients.
ServerCredentials
An object with factory methods fro creating credential objects for servers.
FAQs
gRPC Library for Node
The npm package grpc receives a total of 143,998 weekly downloads. As such, grpc popularity was classified as popular.
We found that grpc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.