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

waitsocket

v1.0.0-rc.4

Published

Library for better handling WebSocket interactions and waiting for response messages

Downloads

21

Readme

WaitSocket.js

Build Status codecov Quality Gate Status

Simplifies communication over WebSocket.

Features

  • Serialization/Deserialization out of the box

  • More structured way to exchange messages (separates type from payload)

    waitSocket.sendMessage('MESSAGE_TYPE', { somePayload: 'example' });
  • Convenient way to handle incoming messages:

    waitSocket.on('MESSAGE_TYPE', (payload) => doSomething(payload));
  • Ability to use a request/response paradigm with WebSockets (mechanism described below):

    const { payload } = await waitSocket.sendRequest('MESSAGE_TYPE', requestPayload);
  • JSONSchema validation of each type of incoming and outgoing messages:

    waitSocket.validation.incoming.addJSONSchema('MESSAGE_TYPE', jsonSchemaObject);
  • Ability to add interceptors to modify incoming and outgoing messages:

    waitSocket.interceptors.incoming.use((messageObject) => {
      console.log('Let\'s see what we have received', messageObject);
      // And modify the object
      return {
        ...messageObject,
        something: 'new',
      };
    });
  • Flexible message format customization

  • Fully TypeScript

Installation

npm i waitsocket

or

yarn add waitsocket

Usage

Importing

import WaitSocket from 'waitsocket';

Creating instance

const waitSocket = new WaitSocket('ws://my.websocket.server:9000');

Or you can use it with your own instance of WebSocket, or even with some extensions like RobustWebSocket:

const ws = new RobustWebSocket('ws://my.websocket.server:9000');
const waitSocket = new WaitSocket(ws);

JSONSchema Validation

You can define JSONSchema for each type of your incoming and outgoing messages. For incoming messages, validation process original deserialized message (before any interceptors). For outgoing messages, validation process resulting message (after all interceptors, but before serialization, of course).

waitSocket.validation.incoming.addJSONSchema('INCOMING_MESSAGE_TYPE', incomingJSONSchemaObject);
waitSocket.validation.outgoing.addJSONSchema('OUTGOING_MESSAGE_TYPE', outgoingJSONSchemaObject);

Customization

If you wish to use your own message format, you can do it by extending WaitSocket class and overriding these functions, responsible for message construction and parsing:

  • getType(messageObject: MessageType): string - Returns message type.
  • getPayload(messageObject: MessageType): any - Returns message payload.
  • getRequestId(messageObject: MessageType): string - Returns message requestId meta data.
  • getMessageObject(type: string, payload?: any, requestId?: string): MessageType; - Returns message object with type, payload and requestId in it.

Example (using body parameter instead of payload):

class myWaitSocket extends AbstractWaitSocket<MyMessageType> {
  protected getMessageObject(type: string, payload?: any, requestId?: string) {
    const result: DefaultMessageType = { type };
    if (payload) {
      result.body = payload;
    }
    if (requestId) {
      result.meta = { requestId };
    }
    return result;
  }


  public getPayload(messageObject: PlainObject) {
    return messageObject.body;
  }
}