1.8.1 • Published 1 year ago
@open-rpc/client-js v1.8.1
OpenRPC Client JS
A browser-compatible JSON-RPC client with multiple transports:
- EventEmitter
- HTTP/HTTPS
- WebSocket
- PostMessageWindow
- PostMessageIframe
import { RequestManager, HTTPTransport, Client } from "@open-rpc/client-js";
const transport = new HTTPTransport("http://localhost:8545");
const client = new Client(new RequestManager([transport]));
const result = await client.request({method: "addition", params: [2, 2]});
// => { jsonrpc: '2.0', id: 1, result: 4 }
Examples
import { EventEmitter } from "events";
import { RequestManager, EventEmitterTransport, Client } from "@open-rpc/client-js";
const chan1 = "chan1";
const chan2 = "chan2";
const emitter = new EventEmitter();
const transport = new EventEmitterTransport(emitter, chan1, chan2);
const requestManager = new RequestManager([transport]);
const client = new Client(requestManager);
// event emitter server code
emitter.on(chan1, (jsonrpcRequest) => {
const res = {
jsonrpc: "2.0",
result: "potato",
id: jsonrpcRequest.id,
};
emitter.emit(chan2, JSON.stringify(res));
});
const main = async () => {
const result = await client.request({method: "addition", params: [2, 2]});
console.log(result);
};
main().then(() => {
console.log("DONE");
});
import { RequestManager, Client, HTTPTransport } from "@open-rpc/client-js";
const transport = new HTTPTransport("http://localhost:3333");
const requestManager = new RequestManager([transport]);
const client = new Client(requestManager);
const main = async () => {
const result = await client.request({method: "addition", params: [2, 2]});
console.log(result);
};
main().then(() => {
console.log("DONE");
});
import { RequestManager, Client, WebSocketTransport } from "@open-rpc/client-js";
const transport = new WebSocketTransport("ws://localhost:3333");
const requestManager = new RequestManager([transport]);
const client = new Client(requestManager);
const main = async () => {
const result = await client.request({method: "addition", params: [2, 2]});
console.log(result);
};
main().then(() => {
console.log("DONE");
client.close();
});
Contributing
How to contribute, build and release are outlined in CONTRIBUTING.md, BUILDING.md and RELEASING.md respectively. Commits in this repository follow the CONVENTIONAL_COMMITS.md specification.
1.8.1
1 year ago
1.8.0
1 year ago
1.7.1
3 years ago
1.7.0
3 years ago
1.6.3
3 years ago
1.6.2
3 years ago
1.6.1
3 years ago
1.6.0
4 years ago
1.5.2
4 years ago
1.5.1
4 years ago
1.5.0
4 years ago
1.4.0
4 years ago
1.3.3
4 years ago
1.3.2
4 years ago
1.3.1
4 years ago
1.3.0
4 years ago
1.2.5
4 years ago
1.2.4
5 years ago
1.2.3
5 years ago
1.2.2
5 years ago
1.2.1
5 years ago
1.2.0
5 years ago
1.1.1
5 years ago
1.1.0
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago
0.0.0-development
5 years ago