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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ws-rpc-messaging

v1.0.3

Published

json rpc client and server for data transfer via websocket

Readme

WS-RPC-MESSAGING

WS-RPC-MESSAGING package provides json-rpc 2.0 like way to real-time and bidirectional communication via websocket. I'am trying to keep package simple and lightweight. WS package used in core of current package.

Version npm Build Status

Table of contents

Installing

npm install --save ws-rpc-messaging

Examples

For working examples and details see examples

API

Class: Server

Server class is built using WebSocket.Server from ws package. It has the same constructor. Server only wraps incoming connections with rpc client api.

Server proxy all properties and methods to WebSocket.Server

Exclude Server.clients, returns Set of RPC Clients;

const { Server, NOT_FOUND_ERROR, INTERNAL_ERROR } = require('ws-rpc-messaging');

const fns = {
    sum: (...args) => args.reduce((acc, n) => acc + n, 0),
    sub: (...args) => args.slice(1).reduce((acc, n) => acc - n, args[0]),
};

const handleRequest = ({ id, method, params }, origin) => {
    if (typeof fns[method] === 'function') {
        try {
            origin.respond(id, fns[method](...params));
        } catch (error) {
            origin.throw(id, INTERNAL_ERROR);
        }
    } else {
        origin.throw(id, NOT_FOUND_ERROR);
    }
};

const handleConnection = (client) => {
    /**
     * You can use routing or write any logic you want here...
     * Just don't forget to send response or error if request.id !== undefined
     */
    client.on('request', handleRequest);
};

const server = new Server({ port: 3000 });
server.on('connection', handleConnection);

Class: Client

Client class is built using WebSocket from ws package. It has the same constructor.

Client proxy all properties and methods to WebSocket.Server. (exclude rarely used properties, but also available Client.ws[any_real_prop])

Event (client): 'error'

  • error {Error}
  • client {Client}, self-link for client

Emitted when can't parse received message

Event (client): 'notify'

  • request {Object} json-rpc 2.0 Request object

Emitted when notification message received

Event (client): 'request'

  • request {Object} json-rpc 2.0 Request object
  • client {Client}, self-link for client

Emitted when request message received

Event (client): 'response'

  • request {Object} json-rpc 2.0 Response object
  • client {Client}, self-link for client

Emitted when response message received. Result or error messages.

client.notify(method, params)

  • method {String} json-rpc 2.0 method
  • params {Object} json-rpc 2.0 params

Send notification message to receiver. Receiver emit 'notify' event.

Notification DO NOT expect to have a response!

client.request(method, params, callback)

  • method {String} json-rpc 2.0 method
  • params {Object} json-rpc 2.0 params
  • callback (error, result, origin)
    • error: json-rpc 2.0 error or null
    • result: json-rpc 2.0 result or undefined
    • origin: self-link for client

Send request message to receiver. Receiver emit 'request' event. Callback will be fired with JSON-RPC 2.0 RESPONSE object and null error or with JSON-RPC 2.0 ERROR object.

client.bulk(messages)

  • messages {Array} of notify { method, params } or request { method, params, callback } objects.

Send many requests and notifications to receiver. Receiver handle every message independently and send response if callback provided.

client.respond(id, result)

  • id {String} json-rpc 2.0 request id, (string, int, null)
  • result {Object} json-rpc 2.0 result

Send response message to request sender. Fires callback provided by the Client.request with the same id as in request.

client.throw(id, error)

  • id {String} json-rpc 2.0 request id, (string, int, null)
  • error {Object} json-rpc 2.0 error

Send error message to request sender. Fires callback provided by the Client.request with the same id as in request.

const { Client } = require('ws-rpc-messaging');

const client = new Client('ws://localhost:3000');

client.on('open', () => {
    client.request('sum', [1, 3, 5], (err, res) => {
        console.log(err, res);
    });

    client.request('sub', [10, 2, 3], (err, res) => {
        console.log(err, res);
    });

    client.request('multiply', [2, 2, 3], (err, res) => {
        console.log(err, res);
    });
});

Broadcast

For more information check https://github.com/websockets/ws#server-broadcast

const { Server, OPEN } = require('ws-rpc-messaging');

const server = new Server({ port: 3000 });

const messages = [];

const handleNotify = ({ method, params }) => {
    if (method === 'messages.write') {
        messages.push(params.message);

        /** broadcast new message to all connected clients */
        server.clients.forEach((client) => {
            if (client.readyState === OPEN) {
                client.notify('messages.new', params);
            }
        });
    }
};

Usefull links