Using clients
On Node.js, you use the same clients as you do with Connect for Web, but with a transport from @connectrpc/connect-node instead of from @connectrpc/connect-web:
import { createClient } from "@connectrpc/connect";
import { createConnectTransport } from "@connectrpc/connect-node";
const transport = createConnectTransport({
httpVersion: "1.1",
baseUrl: "http://demo.connectrpc.com",
});
async function main() {
const client = createClient(ElizaService, transport);
const res = await client.say({
sentence: "I feel happy.",
});
console.log(res.sentence);
}
void main();
For details on clients (including error handling, interceptors, and accessing headers and trailers), please refer to the documentation for Web.
Under the hood, the transports from @connectrpc/connect-node
use the built-in Node modules http
, https
, and http2
instead of the fetch
API. With HTTP/2, clients can use the Connect, gRPC, or gRPC-Web protocol, and
call all types of RPCs. With HTTP 1.1, the gRPC protocol and bidirectional
streaming are not supported.
Connect
The function createConnectTransport()
creates a transport for the Connect
protocol.
The most important options for the Connect transport are as follows:
import { createConnectTransport } from "@connectrpc/connect-node";
const transport = createConnectTransport({
// Requests will be made to <baseUrl>/<package>.<service>/method
baseUrl: "https://demo.connectrpc.com",
// You have to tell the Node.js http API which HTTP version to use.
httpVersion: "2",
// Interceptors apply to all calls running through this transport.
interceptors: [],
});
gRPC
The function createGrpcTransport()
creates a transport for the gRPC protocol.
The most important options for the gRPC transport are as follows:
import { createGrpcTransport } from "@connectrpc/connect-node";
const transport = createGrpcTransport({
// Requests will be made to <baseUrl>/<package>.<service>/method
baseUrl: "https://demo.connectrpc.com",
// Interceptors apply to all calls running through this transport.
interceptors: [],
});
The gRPC transport requires HTTP/2.
gRPC-web
The function createGrpcWebTransport()
creates a Transport for the gRPC-web
protocol. Any gRPC service can be made available to gRPC-web with the
Envoy Proxy. ASP.NET Core supports gRPC-web with
a middleware.
Connect for Node and connect-go
support gRPC-web out of the box.
import { createGrpcWebTransport } from "@connectrpc/connect-node";
const transport = createGrpcWebTransport({
// Requests will be made to <baseUrl>/<package>.<service>/method
baseUrl: "https://demo.connectrpc.com",
// You have to tell the Node.js http API which HTTP version to use.
httpVersion: "2",
// Interceptors apply to all calls running through this transport.
interceptors: [],
});