0.7.2 • Published 2 years ago

jsonrpc-websocket-client v0.7.2

Weekly downloads
767
License
ISC
Repository
github
Last release
2 years ago

jsonrpc-websocket-client

Package Version Build Status PackagePhobia Latest Commit

JSON-RPC 2 over WebSocket

Install

Installation of the npm package:

> npm install --save jsonrpc-websocket-client

Usage

import Client from "jsonrpc-websocket-client";

async function main() {
  const client = new Client("ws://example.org");

  console.log(client.status);
  // → closed

  await client.open();

  console.log(client.status);
  // → open

  console.log(await client.call("method", [1, 2, 3]));

  await client.close();
}

// Run the main function and prints any errors.
main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Creation

const client = new Client(opts);

opts is either a string (the URL of the server) or an object with the following properties:

  • url: URL of the JSON-RPC server
  • protocols (optional): the WebSocket sub-protocols to use
  • rejectUnauthorized (defaults to true): whether to reject invalid HTTPS certificate (e.g. self signed)

Connection management

Status

console.log(client.status);

Possible values:

  • open
  • connecting
  • closed

Connection

await client.open();

Disconnection

await client.close();

This method can also be used to abort the connection while connecting.

Events

Connection

client.on("open", () => {
  console.log("client is now open");
});

Disconnection

client.on("closed", () => {
  console.log("client is now closed");
});

Notification

client.on("notification", (notification) => {
  console.log("notification received", notification);
});

Recipes

Always stay connected

Reconnect on disconnection:

client.on("closed", () => {
  client.open();
});

Use back off to keep retrying to connect:

import { createBackoff } from "jsonrpc-websocket-client";

client.open(createBackoff());

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet

0.7.2

2 years ago

0.7.1

2 years ago

0.7.0

2 years ago

0.6.0

3 years ago

0.5.0

5 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.1-5

8 years ago

0.0.1-4

8 years ago

0.0.1-3

8 years ago

0.0.1-2

8 years ago

0.0.1-1

8 years ago

0.0.1-0

8 years ago