anysocket

    0.4.9 • Public • Published

    AnySocket

    An abstract networking layer over multiple transports, client/server agnostic with support for E2EE

    NPM version Downloads

    Important

    This is a work in progress and API is subject to change.

    Features

    • Client / Server agnostic
    • Support for request/reply
    • Custom AUTH method
    • E2EE between peers with forward secrecy
    • RPC support
    • P2P using a proxy server (with support for direct E2EE between peers)
    • Binary support (see: AnySocket.Packer)
    • Browser support - 31kb footprint (see: /dist/anysocket.browser.js)
    • Multiple transports *(implemented atm: ws/wss, http/https)
    • All peers have a UUIDv4 associated
    • Disconnect detection using a heartbeat
    • Not Battle Tested ...yet

    ** http transport is experimental

    Info: Binary RPC arguments and responses are auto packed/unpacked (AnySocket.Packer.pack/AnySocket.Packer.unpack).

    Benchmarks

    See benchmarks

    Installation

    npm install --save anysocket

    or

    <script src="/dist/anysocket.browser.js"></script>

    or (if using AnySocketHTTP)

    <script src="@anysocket"></script>

    How to use

    The following example starts a websocket server on port 3000.

    const AnySocket = require("anysocket");
    const server = new AnySocket();
    const PORT = 3000;
    server.listen("ws", PORT)
        .then(() => {
            console.log("Listening on port:", PORT);
        })
        .catch((err) => {
            console.error("Failed to start server:", err);
        });
    server.on("connected", (peer) => {
        console.log("Connected", peer.id);    
        peer.send({
            hello: "world"
        });
    });
    server.on("message", (packet) => {
        console.log("From:", packet.peer.id, "Message:", packet.msg);
    });
    server.on("disconnected", (peer, reason) => {
        console.log("Disconnected", peer.id, "Reason:", reason);
    });

    The following example connects to a websocket on port 3000

    const AnySocket = require("anysocket");
    const client = new AnySocket();
    const PORT = 3000;
    client.connect("ws", "127.0.0.1", PORT)
        .then(() => {
            console.log("Connected to server");
        })
        .catch((err) => {
            console.error("Failed to connect to server:", err);
        });
    
    // after negotiating the AUTH packet, it will trigger the connect event
    client.on("connected", (peer) => {
        console.log("Connected", peer.id);    
        peer.send({
            hello: "world"
        });
    });
    client.on("message", (packet) => {
        console.log("From:", packet.peer.id, "Message:", packet.msg);
    });
    client.on("disconnected", (peer, reason) => {
        console.log("Disconnected", peer.id, "Reason:", reason);
    });

    More in the examples folder.

    Documentation

    See documentation and examples folder

    Upcoming Features

    • Mesh Network
    • Multiple transports: tcp, udp*, ipc
    • Client reconnection

    * this will require a change in the protocol, as the protocol assumes the packets are sent using a reliable, ordered connection

    License

    MIT

    Install

    npm i anysocket

    DownloadsWeekly Downloads

    17

    Version

    0.4.9

    License

    MIT

    Unpacked Size

    182 kB

    Total Files

    67

    Last publish

    Collaborators

    • lynxaegon