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

node-red-contrib-json-rpc-processor

v1.0.2

Published

A Node-RED node to process JSON RPC 2.0 messages

Downloads

161

Readme

node-red-contrib-json-rpc-processor

A Node-RED node to process and validate JSON RPC 2.0 messages.

Purpose

JSON RPC 2.0 is a lightweight request/response protocol that is well suited to process incoming messages in the Node-RED environement.

There are already JSON RPC 2.0 related nodes like node-red-contrib-jsonrpc. That node wraps the node-jsonrpc2 library which bakes the transport (http, tcp) within the library.

I wanted tp create a node that is transport agnostic to leverage the different transport nodes that are available within Node-RED's core library (http, tcp, udp, websockets, mqtt). The result is node-red-contrib-json-rpc-processor, it's role is to parse and validate incoming messages as well as format responses.

Usage

The node is used in a similar fashion as some of the parser nodes like the JSON node, one instance is used after the transport to parse and validate the message and another instance is used before the response to format the outgoing message.

server

Specifying methods

In order to work, the node needs to know which method it needs to process and validate. The methods to process are passed to the node using the msg.methods property. Each method has a JSON Schema attached to it that will allow the node to validate the method's parameters. Here is a simple example that defines two methods:

{
  "getRandomAnimal": {
    "request": {
      "title": "getRandomAnimal",
      "type": "object"
    }
  },
  "add": {
    "request": {
      "title": "add",
      "type": "object",
      "properties": {
        "number1": {
          "type": "number",
          "minimum": 0
        },
        "number2": {
          "type": "number",
          "minimum": 0
        }
      },
      "required": [
        "number1",
        "number2"
      ]
    }
  }
}

In the above example, we define two methods, getRandomAnimal and add. The getRandomAnimal method doesn't have any parameters so we simply define an empty schema ({"type": "object"}) in the request property of the method.

For the add method we specify two parameters, number1 and number2 which are both of type number with a minimum value of 0. Note also that both properties are required.

Processing an incoming message

When processing an incoming message, if the message is valid the parameters will be sent on the first output as msg.payload. If the message is not valid (badly formatted JSON RPC 2.0 or parameters that don't validate against the JSON Schema) a JSON RPC 2.0 error will be sent out of the second output which can be sent directly as a response.

A valid message will also have it's method in msg.rpcMethod and extra information like the id of the message in msg.rpcData.

The rpcMethod would typically be used to route the message to a function, flow or subflow using a switch node.

Processing the parameters

The actual processing of the parameters can be done any way the user wants, could be a normal flow, a function, a subflow etc. The result needs to be in msg.payload and sent to the second node-red-contrib-jsonrpc-processor where the response will be formatted. It is important the message still has the properties originating from the first node (msg.rpcData and msg.rpcMethod) for the node to work properly.

If the user needs to generate an error he can use a function node with the usual node.error('Error message', msg) mechanism along with a catch node connected to the second node-red-contrib-jsonrpc-processor. The node will automatically format the error.