Code Monkey home page Code Monkey logo

jsonrpc2-ws's Introduction

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

❤️

BTC: 1CsARqdT2PDLdWng8r2h5pzmyC6xkVnxKw

License

MIT

jsonrpc2-ws's People

Contributors

kanreisa avatar mugeso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mugeso

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.