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

@savant/ws-middleware

v1.0.38

Published

Add remote WebSocket support to any pre-existing class or function while retaining existing syntax

Downloads

88

Readme

@savant/ws-middleware

@savant/ws-middleware is a library that enables you to add remote WebSocket support to any pre-existing class or function, while still retaining the existing syntax. This is particularly useful when you want to use the same syntax in both local and remote contexts.

ws-middleware achieves this by wrapping the class or function with a WebSocket server, which then proxies the calls to the remote context. This allows you to use the same syntax in both the local and remote contexts, without having to worry about the underlying communication mechanism.

See examples below for further explanation

Installation

To install @savant/ws-middleware, run the following command:

npm install @savant/ws-middleware

Usage

Here's an example of how to use @savant/ws-middleware with a class:


class Animal {
    constructor(name) {
        console.log("(...Animal) constructed", name);
        this.name = name;
    }

    getName (name) {
        return name;
    }

    setAge (age) {
        console.log("(...Animal) age set: " + age);
        this.age = age;
    }

    async wait10Seconds () {
        return new Promise(r => setTimeout(() => {r("waited 10 seconds!")}, 10000));
    }
}


// Host / Server
(async () => {
    const { Host } = require("@savant/ws-middleware");
    const server = new Host(Animal);
    server.listen(3000);
})();


// Client
(async () => {
    const { Client } = require("@savant/ws-middleware");

    // Wrap our Animal class with a WebSocket client
    const RemoteAnimal = Client(Animal, "ws://127.0.0.1:3000");

    const remoteAnimal = new RemoteAnimal("dog");
    const result = await remoteAnimal.wait10Seconds(10);
    console.log("Result:", result); // prints "Result: waited 10 seconds!"
})();

On the "host" or "server" side, we use the Host function provided by @savant/ws-middleware to wrap the Animal class with a WebSocket server. On the "client" side, we use the Client function to create a proxy to the remote Animal class. We can then use this proxy as if it were a local instance of the Animal class, while the underlying communication is handled transparently by @savant/ws-middleware. Here's an example of how to use @savant/ws-middleware with a function:

function add(a, b) {
    return a + b;
}

// Host / Server
(async () => {
    const { Host } = require("@savant/ws-middleware");
    const server = new Host(add);
    server.listen(3000);
})();

// Client
(async () => {
    const { Client } = require("@savant/ws-middleware");
    const remoteAdd = Client(add, "ws://127.0.0.1:3000");
    const result = remoteAdd(1, 2);
    console.log("Result:", result); // prints "Result: 3"
})();

Additional Examples

async function getData(url) {
    const response = await fetch(url);
    return response.json();
}

// Host / Server
(async () => {
    const { Host } = require("@savant/ws-middleware");
    const server = new Host(getData);
    server.listen(3000);
})();

// Client
(async () =>
    const { Client } = require("@savant/ws-middleware");
    const remoteGetData = Client(getData, "ws://127.0.0.1:3000");

    const data = await remoteGetData("https://api.example.com/data");
    console.log(data); // prints the JSON data returned by the API
})();