Coder Social home page Coder Social logo

facuz / client-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from open-rpc/client-js

1.0 1.0 0.0 1.56 MB

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

License: Apache License 2.0

JavaScript 0.90% TypeScript 97.98% HTML 1.11%

client-js's Introduction

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.

client-js's People

Contributors

agostbiro avatar belfordz avatar dependabot-preview[bot] avatar devonwesley avatar meowsbits avatar openrpc-bastion avatar semantic-release-bot avatar shanejonas avatar stevanlohja avatar zcstarr avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.