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

rpc2-websocket-client

v1.1.5

Published

<p align="center"> <a href="https://github.com/radarsu/rpc-websocket-client/" target="blank"><img src="https://raw.githubusercontent.com/radarsu/rpc-websocket-client/master/assets/logo.png" alt="rpc-websocket-client" /></a><br/> <strong>Fast JSON

Downloads

3

Readme

"Buy Me A Coffee"

Description

I really lacked typescript support or type definitions of rpc-websockets. I kept everything as simple as possible for best performance and in principle stay close to the metal. Under the hood id-generation for requests is done using uuid/v1 to provide id uniqueness as an additional feature.

Installation

Using npm:

npm i rpc2-websocket-client

Using CDN (on browsers)

<script src="https://cdn.jsdelivr.net/npm/rpc2-websocket-client/dist/rpc-websocket-client.umd.full.js"></script>

Features

  • TypeScript with documentation in comments.
  • Unique RPC identifiers by uuid/v1.
  • Lightweight. Allows you to call noRpc() method to prevent sending jsonrpc: '2.0' overhead from all messages if you'd like to ignore the JSON RPC 2.0 standard for better performance.
  • Option to connect RpcWebSocketClient with already existing WebSocket with changeSocket() and listenMessages() methods. Useful if you use REST or GraphQL implementation from another library and want to handle JSON RPC 2.0 when communicating from server to client (that was my use case to develop this package).

Basic Usage

// vite 使用'rpc-websocket-client.umd.full',解决require is not defined问题
// import {RpcWebSocketClient} from 'rpc2-websocket-client/dist/rpc-websocket-client.umd.full';
// vite.config.ts 增加以下配置
// export default {
//     optimizeDeps: {
//         include: ['rpc2-websocket-client/dist/rpc-websocket-client.umd.full']
//     }
// };
import { RpcWebSocketClient } from 'rpc2-websocket-client';

(async () => {

    const rpc = new RpcWebSocketClient();
    await rpc.connect(`ws://localhost:4000/`);
    // Connection is established now.

    // Let's hope there will be no error or it will be catched in some wrapper.
    await rpc.call(`auth.login`, [`rpcMaster`, `mySecretPassword`]);

    // Now lets be pesimistic.
    const res = await rpc.call(`auth.login`, [`rpcMaster`, `mySecretPassword`]).then(() => {
        // Woohoo, user logged!
    }).catch((err) => {

        // Err is typeof RpcError (code: number, message: string, data?: any).
        await rpc.call(`auth.signup`, {
            login: `rpcMaster`,
            password: `mySecretPassword`,
        });

        return false;
    });

    // If catch wrapper returned false, let's not continue.
    if (res === false) {
        return;
    }

    rpc.notify(`btw.iHateYou`, [`over and out`]);

    // Close the connection by using native ws.close().
    rpc.ws.close();

})();

Advanced Usage

import { RpcWebSocketClient } from 'rpc-websocket-client';

(async () => {
    // lets say you use WebSocket implementation for GraphQL Client -> Server communication
    // e.g. Apollo, and it's already connected
    // but you want to handle some of the Server -> Client communication with RPC

    const ws = (apollo as any).client.wsImpl;
    const rpc = new RpcWebSocketClient();

    rpc.onRequest.push((data) => {       // data is typeof RpcRequest
        // controller-like stuff
    });

    rpc.onNotification.push((data) => {  // data is typeof RpcNotification
        // notification handling
    });

    // here goes magic for listening to already-connected socket
    rpc.changeSocket(ws);
    rpc.listenMessages();
})();