New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jsonrpc2-ws

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonrpc2-ws

Simple, Fast, Robust Implementation of JSON-RPC 2.0 over WebSocket for Node.js w/ TypeScript

  • 1.0.0-beta24
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
638
decreased by-4.92%
Maintainers
1
Weekly downloads
 
Created
Source

jsonrpc2-ws

Simple, Fast, Robust Implementation of JSON-RPC 2.0 over WebSocket for Node.js w/ TypeScript

npm build

Installation

npm install jsonrpc2-ws --save

How to use

Standalone

// TypeScript
import { Server as RPCServer } from "jsonrpc2-ws";

const rpc = new RPCServer({
    wss: {
        port: 3000
    }
});

rpc.on("connection", (socket, req) => {
    console.log(`${socket.id} connected!`);

    socket.on("close", () => {
        console.log(`${socket.id} disconnected!`);
    });

    rpc.broadcast("count", { count: rpc.sockets.size });

    // room
    socket.joinTo("general");
    rpc.notifyTo("general", "general.count", { count: rpc.in("general").size });
});

rpc.methods.set("nick", (socket, params) => {
    // socket#data is Map to store custom data.
    socket.data.set("nick", params.nick);
});

rpc.methods.set("join", (socket, params) => {
    if (socket.joinTo(params.ch) === true) {
        rpc.notifyTo(params.ch, `${params.ch}.count`, { count: rpc.in(params.ch).size });
        return;
    } else {
        throw new Error("Already joined");
    }
});

rpc.methods.set("chat", (socket, params) => {
    if (!params || !params.ch || !params.message) {
        throw new Error("Invalid request");
    }

    rpc.notifyTo(params.ch, "chat", {
        time: Date.now(),
        id: socket.id,
        nick: socket.data.get("nick") || "anonymous",
        ch: params.ch,
        message: params.message
    });
});

// note: rpc method supports async/await or Promise.
rpc.methods.set("something-async-method", async (socket, params) => {
    const res = await somethingAsyncMethod();
    return res;
});

w/ HTTP server

// TypeScript
import * as http from "http";
import { Server as RPCServer } from "jsonrpc2-ws";

const server = http.createServer();
const rpc = new RPCServer({ wss: { server } });

w/ Express

// TypeScript
import express = require("express");
import * as http from "http";
import { Server as RPCServer } from "jsonrpc2-ws";

const app = express();
const server = http.createServer(app);
const rpc = new RPCServer({ wss: { server } });

Compatibility

:heart:

BTC: 1CsARqdT2PDLdWng8r2h5pzmyC6xkVnxKw

License

MIT

Keywords

FAQs

Package last updated on 10 Nov 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc