Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
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.
Copyright 2015 Google Inc.
#Documentation
You can find more detailed documentation and examples in the doc and examples directories respectively.
#Installation
See INSTALL for installation instructions for various platforms.
#Repository Structure & Status
This repository contains source code for gRPC libraries for multiple languages written on top of shared C core library [src/core] (src/core).
Libraries in different languages are in different states of development. We are seeking contributions for all of these libraries.
Language | Source | Status |
---|---|---|
Shared C [core library] | [src/core] (src/core) | Beta - the surface API is stable |
C++ | [src/cpp] (src/cpp) | Beta - the surface API is stable |
Ruby | [src/ruby] (src/ruby) | Beta - the surface API is stable |
NodeJS | [src/node] (src/node) | Beta - the surface API is stable |
Python | [src/python] (src/python) | Beta - the surface API is stable |
PHP | [src/php] (src/php) | Beta - the surface API is stable |
C# | [src/csharp] (src/csharp) | Beta - the surface API is stable |
Objective-C | [src/objective-c] (src/objective-c) | Beta - the surface API is stable |
See MANIFEST.md for a listing of top-level items in the repository.
#Overview
Remote Procedure Calls (RPCs) provide a useful abstraction for building distributed applications and services. The libraries in this repository provide a concrete implementation of the gRPC protocol, layered over HTTP/2. These libraries enable communication between clients and servers using any combination of the supported languages.
##Interface
Developers using gRPC typically start with the description of an RPC service (a collection of methods), and generate client and server side interfaces which they use on the client-side and implement on the server side.
By default, gRPC uses Protocol Buffers as the Interface Definition Language (IDL) for describing both the service interface and the structure of the payload messages. It is possible to use other alternatives if desired.
###Surface API Starting from an interface definition in a .proto file, gRPC provides Protocol Compiler plugins that generate Client- and Server-side APIs. gRPC users typically call into these APIs on the Client side and implement the corresponding API on the server side.
Synchronous RPC calls, that block until a response arrives from the server, are the closest approximation to the abstraction of a procedure call that RPC aspires to.
On the other hand, networks are inherently asynchronous and in many scenarios, it is desirable to have the ability to start RPCs without blocking the current thread.
The gRPC programming surface in most languages comes in both synchronous and asynchronous flavors.
gRPC supports streaming semantics, where either the client or the server (or both) send a stream of messages on a single RPC call. The most general case is Bidirectional Streaming where a single gRPC call establishes a stream where both the client and the server can send a stream of messages to each other. The streamed messages are delivered in the order they were sent.
#Protocol
The gRPC protocol specifies the abstract requirements for communication between clients and servers. A concrete embedding over HTTP/2 completes the picture by fleshing out the details of each of the required operations.
A gRPC RPC comprises of a bidirectional stream of messages, initiated by the client. In the client-to-server direction, this stream begins with a mandatory Call Header
, followed by optional Initial-Metadata
, followed by zero or more Payload Messages
. The server-to-client direction contains an optional Initial-Metadata
, followed by zero or more Payload Messages
terminated with a mandatory Status
and optional Status-Metadata
(a.k.a.,Trailing-Metadata
).
The abstract protocol defined above is implemented over HTTP/2. gRPC bidirectional streams are mapped to HTTP/2 streams. The contents of Call Header
and Initial Metadata
are sent as HTTP/2 headers and subject to HPACK compression. Payload Messages
are serialized into a byte stream of length prefixed gRPC frames which are then fragmented into HTTP/2 frames at the sender and reassembled at the receiver. Status
and Trailing-Metadata
are sent as HTTP/2 trailing headers (a.k.a., trailers).
gRPC inherits the flow control mechanisms in HTTP/2 and uses them to enable fine-grained control of the amount of memory used for buffering in-flight messages.
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
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
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.