npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mahsumurebe/jrpc-client

v2.2.0

Published

JSONRPC 2.0 NodeJS Client written in TypeScript

Downloads

81

Readme

JRPC Client

JSONRPC 2.0 Client package for Nodejs. Fully tested to comply with the official JSON-RPC 2.0 specification

GitHub package.json version GitHub release (latest by date) GitHub tag (latest by date) npm

Libraries.io SourceRank, scoped npm package minzipped-size minfied-size

issues-open issues-closed license

Quick Overview

It is used to quickly create JSONRPC Client. Method call is very simple.

To Create a Server

You can use the @mahsumurebe/jrpc-server package to create a server.

Install

For installation, you can run the following command in the directory of your project.

npm install @mahsumurebe/jrpc-client  

Usage

It should not create a JRPCClient instance.

import { JRPCClient, HttpAdapter } from '@mahsumurebe/jrpc-client';

// Create instance
const clientInstance = new JRPCClient(new HttpAdapter('http://localhost:3000'));
// Call start method for connection
await clientInstance.start();

Call Method

Method calls are made after the JRPCClient instance is created.

// Call foo method with "bar" and "baz" parameters
const response = await clientInstance.call({
  id: 1,
  jsonrpc: "2.0",
  method: "foo",
  params: ["bar", "baz"],
});
console.log(response);
// Response Object
// { id: 1, jsonrpc: "2.0", response: "foo response" }

The call method returns the JSONRPC response. Returns the ErrorResponse class instance if the request response is an error.

Batch Call Method

Call method can be used to call requests. You can review the usage example below.

const batchResponse = await clientInstance.call([
  { id: 1, jsonrpc: "2.0", method: "foo", params: ["bar", "baz"] },
  { id: 2, jsonrpc: "2.0", method: "bar", params: ["bar", "baz"] },
  { jsonrpc: "2.0", method: "notification", params: ["bar", "baz"] }, // Notification request does not return value
]);
console.log(batchResponse);
// [
//   { id: 1, jsonrpc: "2.0", response: "foo response" },
//   { id: 2, jsonrpc: "2.0", response: "bar response" },
// ]

Notification Method

Check out the sample code below to make a method notification.

// Notification foo method with "bar" and "baz" parameters
await clientInstance.notification({
  jsonrpc: "2.0",
  method: "foo",
  params: ["bar", "baz"],
});

Note: The notification method does not return any response. The request is sent to the server. No response is expected from the server.

Batch Notification Method

Check out the example below for sending batch notifications.

await clientInstance.notification([
  { jsonrpc: "2.0", method: "foo", params: ["bar", "baz"] },
  { jsonrpc: "2.0", method: "bar", params: ["bar", "baz"] },
  { jsonrpc: "2.0", method: "baz", params: ["bar", "baz"] }, 
]);

Adapters

There are HTTP and Websocket adapters available.

HTTP

HTTP Adapter is used to connect to JRPC Servers served over HTTP Protocol.

// Adapter Instance
import {JRPCClient, HttpAdapter} from '@mahsumurebe/jrpc-client';

const adapter = new HttpAdapter('http://localhost:3000');

// Client Instance
const clientInstance = new JRPCClient(adapter);

Configuration List

Configurations are defined in the object in the first parameter of the construction method when creating the HttpAdapter.

| KEY | DEFAULT | DESCRIPTION | Type | |---------|-----------|-----------------|----------| | parser | null | Response parser | Function | | headers | null | HTTP Headers | object | | timeout | 10_000 | Timeout | number |

Websocket

Websocket Adapter is used to connect to JRPC Servers served over Websocket Protocol.

// Adapter Instance
import { JRPCClient, WebsocketAdapter } from '@mahsumurebe/jrpc-client';

const adapter = new WebsocketAdapter('ws:/localhost:3000');

// Client Instance
const clientInstance = new JRPCClient(adapter);

Configuration List

Configurations are defined in the object in the first parameter of the construction method when creating the WebsocketAdapter.

| KEY | DEFAULT | DESCRIPTION | Type | |---------|-----------|-----------------|----------| | parser | null | Response parser | Function | | headers | null | HTTP Headers | object | | timeout | 10_000 | Timeout | number |

Custom Adapters

For custom adapters, you need to extend the adapter class with the AdapterAbstract abstract class. You have to create the abstract functions request, connect and destroy inside the class.

connect: A piece of code that provides the protocol connection should be added to this method.

destroy: A piece of code that breaks the protocol connection should be added to this method.

request: A piece of code that sends the body to the server and parses the output should be added to this method. When there is a response from the server, the response object should be sent to the adapter.config.parse function.

Resources