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

mocksserver-ws-plugin

v0.3.1

Published

A plugin for the Mocks-Server project that provides WebSocket integration. See https://www.mocks-server.org for more information.

Downloads

20

Readme

Mocks-Server Websocket Plugin

This is a plugin for the Mocks-Server project to add Websocket functionality. Please read the Mocks-Server Documentation before proceeding.

Description

The plugin extends the functionality of the Mocks-Server so that WebSocket connections can be used within the application. This has the advantage over a separate WebSocket test server that resources can be shared and WebSockets can be accessed from middleware variants. To achieve this, the plugin provides central management of all existing socket connections, which can be accessed from anywhere in the application.

This software is currently in a very early stage and has not been fully tested. Bugs are to be expected! The functionality is still limited, but meets the requirements to successfully close the gap between WebSockets and your Mock-API.

Installation

To install the plugin, run the following command:

npm install -D mocksserver-ws-plugin

Detailed instructions about the installation process can be found here.

Basic Usage

NOTE: If you are new to the Mocks Server project, I highly recommend reading the complete Mocks-Server Documentation first!

On the first run the Mocks-Server will create a configuration file. We need this file later to register the plugin and the handler classes. More information about this can be found in the quick start guide.

Connections are established using handler classes. A handler object is associated with a route. To create a simple handler class, just extend the BaseHandler from the plugin package. The only method you must implement is the onMessage method, which is called when a message arrive:

// handler.js
const { BaseHandler } = require('mocksserver-ws-plugin');

class FooHandler extends BaseHandler {
  onMessage(message) {
    this.writeMessage(`Message from the client: ${message}`)
  }
}

module.exports = FooHandler

The above handler simply acts as an echo server, which sends the incoming message back to the client with the additional text Message from the client:.

Now we can register the plugin and the handler class in the configuration file. The class reference must be assigned to the route. This is the relative path where the WebSocket is provided by the server.

// mocks.config.js
const { WebsocketPlugin } = require('mocksserver-ws-plugin');
const FooHandler = require('./handler');

module.exports = {
  // ...
  plugins: {
    // Register the plugin
    register: [WebsocketPlugin],
    websocket: {
      // Register the handler class 
      handler: {
        '/foo': FooHandler
      }
    },
    // ...
  },
  // ...
};

After starting the server, the handler can be reached at the following URL:

ws://<host>:<port>/foo

To test this, we can write a simple client script:

// client.js
const { Websocket } = require('ws');

const ws = new WebSocket('ws://<host>:<port>/foo');
ws.on('open', () => {
    ws.send('Hello from client.');
});
ws.on('message', message => {
    console.log(`Server said: ${message}`);
});

The program will print Server said: Message from the client: Hello from client. in the console.

Advanced usage

Each handler object has a reference to the handler collector, which manages the handler objects. At the moment the collector provides two methods to access other handlers: applyOn and applyAll. With applyOn it is possible to call a function on all handlers connected to a specific path.

For example, say you want to send a message received by a handler connected at /foo to all clients of another handlers connected at /bar. To do this you can use the following code snippet:

class FooHandler extends BaseHandler {
  onMessage(message) {
    this.collector.applyOn('/bar', barHandler => {
      barHandler.writeMessage(`Message received by FooHandler: ${message}`)
    });
  }
}

If you want to perform an action on all handlers currently connected (e.g. for a broadcast) it is possible to use the applyAll method. It receives just a callback method without the path:

class FooHandler extends BaseHandler {
  onMessage(message) {
    this.collector.applyAll(handler => {
      handler.writeMessage(`Message received by FooHandler: ${message}`)
    });
  }
}

If you want to access the collector elsewhere in the program, for example in a middleware variant, you can do so via the instance property of the plugin class. The instance exposes the collector property named as handlerCollector. The following code snippet shows how to send a POST-Message received by a middleware variant to a WebSocket handler connected at the route /foo:

// mocks/routes/<route-name>.js
const { WebsocketPlugin } = require('mocksserver-ws-plugin')

module.exports = [
  {
    // ...
    method: "POST",
    variants: [
      {
        // ...
        type: "middleware",
        options: {
          middleware: (request) => {         
            const wsPlugin = WebsocketPlugin.instance;
            const { message } = request.body;

            wsPlugin.handlerCollector.applyOn('/foo', fooHandler => {
              fooHandler.writeMessage(`Message received by middleware: ${message}`);
            });

            // ...
          },
        },
      }
    ],
  }
];

For more information about using middleware variants, see the documentation.

More information

If you want to learn more about this plugin, please read the source code. Every functionality is very well described via JSDoc.

Copyright and license

The project is written and documented by Oliver Huckfeldt © 2024. Code and documentation is released under MIT-License.