Socket
Socket
Sign inDemoInstall

@open-rpc/client-js

Package Overview
Dependencies
15
Maintainers
2
Versions
27
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @open-rpc/client-js

A browser-compatible JSON-RPC client with multiple transports.


Version published
Weekly downloads
33K
increased by13.08%
Maintainers
2
Install size
953 kB
Created
Weekly downloads
 

Changelog

Source

1.8.1 (2023-01-18)

Bug Fixes

  • bump node version (0348af2)
  • get tests to pass without callback fn for ws.send (7489794)
  • websocket notifications now can be awaited (1ff6217)

Bug Fixes

Features

  • httptransport: injected fetcher (cd68627)

Readme

Source

OpenRPC Client JS

CircleCI branch Dependabot status Chat on Discord npm GitHub release GitHub commits since latest release

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
EventEmitter
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");
});
HTTP
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");
});
WebSocket
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.

FAQs

Last updated on 18 Jan 2023

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc