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,ResponseandRpcErrorare the messages, andencode(),decode()andread()turn them to and from JSON, checking every rule of the specification.- A
Serviceholds 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 anhttpserver, andHttpClientcalls one. HeaderFraming,LineFramingandMessageFramingtell messages apart in a stream: by aContent-Lengthheader 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:
StdioTransportover the program’s own standard streams,SocketTransportover a socket,ProcessTransportto a child process,WebSocketTransportover a WebSocket, and the two ends of apipe()between isolates. - An
Endpointis 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.
| Name | Kind | Summary |
|---|---|---|
rpc.ChannelTransport | class | Talks over two isolate channels: what it reads arrives on inbox and what it writes goes to outbox. |
rpc.Context | class | What a handler is told about the message it is handling: the request’s id, its method, the endpoint… |
rpc.DEFAULT_BATCH_LIMIT | constant | The most messages a batch may hold before it is refused whole, 1000, until Service.set_batch_limit() says… |
rpc.DEFAULT_MAX_SIZE | constant | The largest message a framing accepts unless told otherwise: 64 MiB. |
rpc.Endpoint | class | One side of a JSON-RPC connection over a transport. |
rpc.HeaderFraming | class | Frames each message with a header block giving its length: |
rpc.HttpClient | class | Calls a JSON-RPC service over HTTP: each call is one POST to url, and its answer is the response. |
rpc.INTERNAL_ERROR | constant | The code for a request that failed while it was being handled, -32603. |
rpc.INVALID_PARAMS | constant | The code for a request whose parameters the method cannot take, -32602. |
rpc.INVALID_REQUEST | constant | The code for valid JSON that is not a valid JSON-RPC message, -32600. |
rpc.LineFraming | class | Frames each message as one line, ended by \n, the framing of newline-delimited JSON. |
rpc.METHOD_NOT_FOUND | constant | The code for a request naming a method the other side does not have, -32601. |
rpc.MessageFraming | class | Takes each piece it is fed as one whole message, for a transport that keeps messages apart itself, such as a… |
rpc.Notification | class | A call that expects no answer: the method to run and its params. |
rpc.PARSE_ERROR | constant | The code for a message that is not valid JSON, -32700. |
rpc.ProcessTransport | class | Talks to a child process over its standard input and output, from os.spawn() with stdin and stdout both… |
rpc.Request | class | A call that expects an answer: the method to run, its params, and the id the answer comes back with. |
rpc.Response | class | The answer to a Request: its id, and either the result the method returned or the error it failed… |
rpc.RpcClosedError | class | Raised by Endpoint.request() when the connection ends before the answer arrives, and by sending on an… |
rpc.RpcError | class | A JSON-RPC error: what a request failed with, on either side of a connection. |
rpc.RpcFramingError | class | Raised when the framing of a stream cannot be read: a header block that is not ASCII, a header without a… |
rpc.RpcHttpError | class | Raised by an HttpClient when the server answers with an HTTP status that carries no JSON-RPC answer:… |
rpc.RpcTimeoutError | class | Raised by Endpoint.request() when its timeout runs out before the answer arrives. |
rpc.SERVER_ERROR_MAX | constant | The highest code JSON-RPC sets aside for errors a server defines itself, -32000. |
rpc.SERVER_ERROR_MIN | constant | The lowest code JSON-RPC sets aside for errors a server defines itself, -32099. |
rpc.Service | class | The methods a program serves, and the answering of every message sent to them. |
rpc.SocketTransport | class | Talks over a connected stream from net: a TcpStream, a UnixStream or a TlsStream, or anything else… |
rpc.StdioTransport | class | Talks over the program’s own standard input and output: what it reads arrives on stdin, and what it writes… |
rpc.WebSocketTransport | class | Talks over a WebSocket from http.websocket, on either end of it: one websocket.accept() returned in a… |
rpc.decode | function | Decodes the JSON text of a message, or of a batch, and reads it as read() does. |
rpc.encode | function | The JSON text of a message, or of a list of messages sent as a batch. |
rpc.endpoint | function | A new Endpoint over transport, framing its messages with a HeaderFraming. |
rpc.http_handler | function | A route handler for an http server that answers JSON-RPC with service: register it for POST on whatever… |
rpc.pipe | function | Two ChannelTransports joined to each other: what one writes, the other reads. |
rpc.process | function | A transport to a child process, over its standard input and output. |
rpc.read | function | Reads a value decoded from JSON as a message: a Request, a Notification or a Response. |
rpc.serve | function | Serves JSON-RPC on a listening socket, across a pool of worker isolates, until it is stopped. |
rpc.socket | function | A transport over a connected socket from net. |
rpc.stdio | function | A transport over the program’s own standard input and output. |
rpc.websocket | function | A transport over a WebSocket from http.websocket. |
Submodules
| Module | Reached as | Summary |
|---|---|---|
rpc.endpoint | rpc.* | Endpoint: one side of a JSON-RPC connection. |
rpc.error | rpc.* | The error codes JSON-RPC defines and the errors rpc raises, kept in a module of their own so every other… |
rpc.framing | rpc.* | How messages are told apart in a stream of bytes. |
rpc.http | rpc.* | JSON-RPC over HTTP, both ends of it. |
rpc.message | rpc.* | The messages JSON-RPC 2.0 exchanges, a Request, a Notification and a Response, and turning them to and… |
rpc.server | rpc.* | Serving JSON-RPC to many connections at once, over TCP, TLS or a Unix domain socket. |
rpc.service | rpc.* | Service: what a program answers. |
rpc.transport | rpc.* | 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 connectedTcpStream,UnixStreamorTlsStream.
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 withstdinandstdoutboth'pipe'.
Returns ProcessTransport
websocket()
rpc.websocket(socket) -> WebSocketTransport
A transport over a WebSocket from http.websocket.
Parameters
socket(http.websocket.WebSocket) — An open WebSocket, fromwebsocket.accept()orwebsocket.connect().
Returns WebSocketTransport
2026, Richard Ore and Zuri contributors