@open-rpc/client-js
    Preparing search index...

    Class Client

    OpenRPC Client JS is a browser-compatible JSON-RPC client with multiple transports and multiple request managers to enable features like round-robin or fallback-by-position.

    import { RequestManager, HTTPTransport, Client } from '@open-rpc/client-js';
    const transport = new HTTPTransport('http://localhost:3333');
    const client = new Client(new RequestManager([transport]));
    const result = await client.request({method: 'addition', params: [2, 2]});
    // => { jsonrpc: '2.0', id: 1, result: 4 }

    Implements

    • IClient
    Index

    Constructors

    Properties

    requestManager: RequestManager

    Methods

    • Close connection

      Returns void

    • Parameters

      • requestObject: Arguments

      Returns Promise<any>

    • Parameters

      • callback: (data: IJSONRPCNotification) => void

      Returns void

    • A JSON-RPC call is represented by sending a Request object to a Server.

      Parameters

      • requestObject: Arguments
        • method

          A String containing the name of the method to be invoked. Method names that begin with the word rpc followed by a period character (U+002E or ASCII 46) are reserved for rpc-internal methods and extensions and MUST NOT be used for anything else.

        • params

          A Structured value that holds the parameter values to be used during the invocation of the method.

      • Optionaltimeout: number

      Returns Promise<any>

      myClient.request({method: "foo", params: ["bar"]}).then(() => console.log('foobar'));
      
    • Initiates [[RequestManager.startBatch]] in order to build a batch call.

      Subsequent calls to [[Client.request]] will be added to the batch. Once [[Client.stopBatch]] is called, the promises for the [[Client.request]] will then be resolved. If the [[RequestManager]] already has a batch in progress, this method is a noop.

      Returns void

      myClient.startBatch();
      myClient.request({method: "foo", params: ["bar"]}).then(() => console.log('foobar'));
      myClient.request({method: "foo", params: ["baz"]}).then(() => console.log('foobaz'));
      myClient.stopBatch();
    • Initiates [[RequestManager.stopBatch]] in order to finalize and send the batch to the underlying transport.

      [[Client.stopBatch]] will send the [[Client.request]] calls made since the last [[Client.startBatch]] call. For that reason, [[Client.startBatch]] MUST be called before [[Client.stopBatch]].

      Returns void

      myClient.startBatch();
      myClient.request({method: "foo", params: ["bar"]}).then(() => console.log('foobar'));
      myClient.request({method: "foo", params: ["baz"]}).then(() => console.log('foobaz'));
      myClient.stopBatch();