Socket
Socket
Sign inDemoInstall

duplex-json-rpc

Package Overview
Dependencies
1
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    duplex-json-rpc

Transport layer independent JSON-RPC library with calls in both directions.


Version published
Maintainers
1
Install size
124 kB
Created

Readme

Source

duplex-json-rpc

Transport layer independent JSON-RPC library with calls in both directions (i.e. the server can perform calls on the client). See the example project for WebSocket usage examples.

Client usage

// Import the package.
import * as JsonRpc from 'duplex-json-rpc';

// Define a custom context type.
type Context = ...;

// Create your client object.
const client = new JsonRpc.Client<Context>((packet, context) => {
    // Define how a string message is sent to the server via
    // your transport layer given a context.
    ...
});

// Perform your JSON RPC call.
const result = await client.call('myMethod', { myParameter: 5 }, myContext);

Server usage

// Import the package.
import * as JsonRpc from 'duplex-json-rpc';

// Define a custom context type.
type Context = ...;

// Define your server methods.
const callHandler: JsonRpc.CallHandler<Context> = {
    async myMethod(params, context): Promise<MyResult> {
        // Optionally throw one of duplex-json-rpc's error types.

        // Return your result.
        return ...;
    }
};

// Create your server object.
const server = new JsonRpc.Server<Context>(callHandler, (packet, context) => {
    // Define how a string message is sent in response to a client
    // RPC call via your transport layer. The target client should
    // be part of the context.
    ...
});

...

// Whenever a client packet is received on your transport layer,
// notify the server object.
await server.onRequestPacket(myReceivedMessage.toString(), myContext);

Duplex server usage

// Import the package.
import * as JsonRpc from 'duplex-json-rpc';

// Define a custom context type.
type Context = ...;

// Define your server methods.
const callHandler: JsonRpc.CallHandler<Context> = {
    async myMethod(params, context): Promise<MyResult> {
        // Optionally throw one of duplex-json-rpc's error types.

        // Return your result.
        return ...;
    }
};

// Create your server object.
const server = new JsonRpc.DuplexServer<Context>(callHandler, (packet, context) => {
    // Define how a string message is sent in response to a client
    // RPC call via your transport layer. The target client should
    // be part of the context.
    ...
});

...

// Whenever a client packet (which may be a request or a response) is received
// on your transport layer, notify the server object.
await server.onDataPacket(myReceivedMessage.toString(), myContext);

// With a DuplexServer, you can now also perform RPC calls on the client.
await server.call('myMethod', { myParameter: 5 }, myContext);

Keywords

FAQs

Last updated on 13 Feb 2022

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