Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

rpc

import rpc

JSON-RPC 2.0: calling methods on another program, and answering its calls, over HTTP, WebSockets, sockets, pipes, or any stream of bytes.

JSON-RPC is a small, stateless protocol for calling methods in another program. It defines the messages and nothing more: which methods exist is up to the programs using it, and the messages travel over whatever connects them. A message is a small JSON object: a request names a method and carries its parameters and an id, the response carries the same id and a result or an error, and a notification is a request nobody answers. Either side may send any of them, so the protocol has no fixed client and server.

The module is built in layers, each usable on its own:

  • Request, Notification, Response and RpcError are the messages, and encode(), decode() and read() turn them to and from JSON, checking every rule of the specification.
  • A Service holds the handler for each method a program serves, and answers every message it is given with them.
  • Over HTTP, http_handler() serves a service from a route of an http server, and HttpClient calls one.
  • HeaderFraming, LineFraming and MessageFraming tell messages apart in a stream: by a Content-Length header in front of each, one to a line, or one to each read of a transport that keeps them apart itself.
  • A transport carries the bytes of a connection: StdioTransport over the program’s own standard streams, SocketTransport over a socket, ProcessTransport to a child process, WebSocketTransport over a WebSocket, and the two ends of a pipe() between isolates.
  • An Endpoint is a service on a connection: it answers what arrives, and calls the other side with requests and notifications of its own.
  • serve() gives every connection to a listening socket an endpoint of its own, across a pool of isolates.
import rpc
import isolate

var ends = rpc.pipe()

isolate.spawn(@(transport) {
  import rpc

  rpc.endpoint(transport)
    .on_request('greet', @(params) => 'Hello, ${params.name}!')
    .serve()
}, ends[1])

var client = rpc.endpoint(ends[0])

echo client.request('greet', { name: 'Zuri' })  # Hello, Zuri!
client.close()

The rpc API

Every public name in rpc, wherever it is declared. Each links to the page that documents it.

NameKindSummary
rpc.ChannelTransportclassTalks over two isolate channels: what it reads arrives on inbox and what it writes goes to outbox.
rpc.ContextclassWhat a handler is told about the message it is handling: the request’s id, its method, the endpoint…
rpc.DEFAULT_BATCH_LIMITconstantThe most messages a batch may hold before it is refused whole, 1000, until Service.set_batch_limit() says…
rpc.DEFAULT_MAX_SIZEconstantThe largest message a framing accepts unless told otherwise: 64 MiB.
rpc.EndpointclassOne side of a JSON-RPC connection over a transport.
rpc.HeaderFramingclassFrames each message with a header block giving its length:
rpc.HttpClientclassCalls a JSON-RPC service over HTTP: each call is one POST to url, and its answer is the response.
rpc.INTERNAL_ERRORconstantThe code for a request that failed while it was being handled, -32603.
rpc.INVALID_PARAMSconstantThe code for a request whose parameters the method cannot take, -32602.
rpc.INVALID_REQUESTconstantThe code for valid JSON that is not a valid JSON-RPC message, -32600.
rpc.LineFramingclassFrames each message as one line, ended by \n, the framing of newline-delimited JSON.
rpc.METHOD_NOT_FOUNDconstantThe code for a request naming a method the other side does not have, -32601.
rpc.MessageFramingclassTakes each piece it is fed as one whole message, for a transport that keeps messages apart itself, such as a…
rpc.NotificationclassA call that expects no answer: the method to run and its params.
rpc.PARSE_ERRORconstantThe code for a message that is not valid JSON, -32700.
rpc.ProcessTransportclassTalks to a child process over its standard input and output, from os.spawn() with stdin and stdout both…
rpc.RequestclassA call that expects an answer: the method to run, its params, and the id the answer comes back with.
rpc.ResponseclassThe answer to a Request: its id, and either the result the method returned or the error it failed…
rpc.RpcClosedErrorclassRaised by Endpoint.request() when the connection ends before the answer arrives, and by sending on an…
rpc.RpcErrorclassA JSON-RPC error: what a request failed with, on either side of a connection.
rpc.RpcFramingErrorclassRaised when the framing of a stream cannot be read: a header block that is not ASCII, a header without a…
rpc.RpcHttpErrorclassRaised by an HttpClient when the server answers with an HTTP status that carries no JSON-RPC answer:…
rpc.RpcTimeoutErrorclassRaised by Endpoint.request() when its timeout runs out before the answer arrives.
rpc.SERVER_ERROR_MAXconstantThe highest code JSON-RPC sets aside for errors a server defines itself, -32000.
rpc.SERVER_ERROR_MINconstantThe lowest code JSON-RPC sets aside for errors a server defines itself, -32099.
rpc.ServiceclassThe methods a program serves, and the answering of every message sent to them.
rpc.SocketTransportclassTalks over a connected stream from net: a TcpStream, a UnixStream or a TlsStream, or anything else…
rpc.StdioTransportclassTalks over the program’s own standard input and output: what it reads arrives on stdin, and what it writes…
rpc.WebSocketTransportclassTalks over a WebSocket from http.websocket, on either end of it: one websocket.accept() returned in a…
rpc.decodefunctionDecodes the JSON text of a message, or of a batch, and reads it as read() does.
rpc.encodefunctionThe JSON text of a message, or of a list of messages sent as a batch.
rpc.endpointfunctionA new Endpoint over transport, framing its messages with a HeaderFraming.
rpc.http_handlerfunctionA route handler for an http server that answers JSON-RPC with service: register it for POST on whatever…
rpc.pipefunctionTwo ChannelTransports joined to each other: what one writes, the other reads.
rpc.processfunctionA transport to a child process, over its standard input and output.
rpc.readfunctionReads a value decoded from JSON as a message: a Request, a Notification or a Response.
rpc.servefunctionServes JSON-RPC on a listening socket, across a pool of worker isolates, until it is stopped.
rpc.socketfunctionA transport over a connected socket from net.
rpc.stdiofunctionA transport over the program’s own standard input and output.
rpc.websocketfunctionA transport over a WebSocket from http.websocket.

Submodules

ModuleReached asSummary
rpc.endpointrpc.*Endpoint: one side of a JSON-RPC connection.
rpc.errorrpc.*The error codes JSON-RPC defines and the errors rpc raises, kept in a module of their own so every other…
rpc.framingrpc.*How messages are told apart in a stream of bytes.
rpc.httprpc.*JSON-RPC over HTTP, both ends of it.
rpc.messagerpc.*The messages JSON-RPC 2.0 exchanges, a Request, a Notification and a Response, and turning them to and…
rpc.serverrpc.*Serving JSON-RPC to many connections at once, over TCP, TLS or a Unix domain socket.
rpc.servicerpc.*Service: what a program answers.
rpc.transportrpc.*Where an endpoint’s bytes come from and go to.

Functions

endpoint()

rpc.endpoint(transport) -> Endpoint

A new Endpoint over transport, framing its messages with a HeaderFraming.

Parameters

  • transport (any) — A transport; see {rpc.transport}.

Returns Endpoint

stdio()

rpc.stdio() -> StdioTransport

A transport over the program’s own standard input and output.

Returns StdioTransport

socket()

rpc.socket(socket) -> SocketTransport

A transport over a connected socket from net.

Parameters

  • socket (any) — A connected TcpStream, UnixStream or TlsStream.

Returns SocketTransport

process()

rpc.process(child) -> ProcessTransport

A transport to a child process, over its standard input and output.

Parameters

  • child (os.Process) — A child spawned with stdin and stdout both 'pipe'.

Returns ProcessTransport

websocket()

rpc.websocket(socket) -> WebSocketTransport

A transport over a WebSocket from http.websocket.

Parameters

  • socket (http.websocket.WebSocket) — An open WebSocket, from websocket.accept() or websocket.connect().

Returns WebSocketTransport


2026, Richard Ore and Zuri contributors