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

@qrvey/websocket-client

v1.0.1

Published

![install size](https://packagephobia.com/badge?p=@qrvey/websocket-client)

Downloads

1,176

Readme

@qrvey/websocket-client

install size

The @qrvey/websocket-client package provides a unified interface to work with websocket as any client. Works from Backend and Frontend. This package is based on the WebSocket services that use Socket.io

Installation

npm install @qrvey/websocket-client

Or with yarn:

yarn add @qrvey/websocket-client

Environment variables

DOMAIN; // The BASE Qrvey domain. If not set, could be pass in the constructor of WebSocketClient class

API Documentation

Class: WebSocketClient

Constructor

new WebSocketClient(auth, options)

Creates an instance of WebSocketClient.

  • Parameters:
    • auth (Object): Authentication details.
      • userId (string, optional): User identifier.
      • clientId (string, optional): Client identifier.
      • token (string, optional): Authentication token.
      • apiKey (string, optional): API key.
    • options (Object): Additional options.
      • domain (string, optional): Domain to stablish connection with the socket. If not set, try to find the domain in process.env.DOMAIN
      • isAutoPingPongEnable (boolean, optional, default=true): Enables or disables the auto ping-pong mechanism.

Methods

publish({ eventName, data, isPublic = false }: { eventName: string; data: Record<string, any>; isPublic?: boolean; }): void

Publishes an event with the specified data.

  • Parameters:
    • eventName (string): The name of the event to publish.
    • data (any): The data to send with the event.
    • isPublic (boolean, optional, default=false): Whether the event is public or not.

subscribe(eventName: string, callback: (message: any) => void): void

Subscribes to an event and provides a callback to handle the messages.

  • Parameters:

    • eventName (string): The name of the event to subscribe to.
    • callback (function): A callback function to handle the messages. The function receives one argument:
      • message (any): The message received from the event.
  • Returns: this (the instance of WebSocketClient)

onError(callback: (message: any) => void): void

Sets a callback function to handle error messages.

  • Parameters:
    • callback (function): A callback function to handle error messages. The function receives one argument:
      • message (any): The error message.

status(): SOCKET_STATUS

Returns the current status of the WebSocket connection.

  • Returns: SOCKET_STATUS (enum): The current status of the WebSocket connection.

connect(): void

Establishes the WebSocket connection if needed. After create the instances, the connection and reconnection is done by default.

disconnect(): void

Disconnects the WebSocket client.

Enum: SOCKET_STATUS

The SOCKET_STATUS enum defines the possible states of a WebSocket connection.

Enum Values:

  • CONNECTED: Indicates that the WebSocket connection has been successfully established.
  • ACTIVE: Indicates that the WebSocket connection is active and currently transmitting or receiving data.
  • RECOVERED: Indicates that the WebSocket connection was lost but has been successfully re-established.
  • DISCONNECTED: Indicates that the WebSocket connection has been intentionally or unintentionally closed.
  • UNDEFINED: Indicates that the WebSocket connection status is undefined or has not yet been determined.

Usage Example

const { WebSocketClient, SOCKET_STATUS }  = require('@qrvey/websocket-client');

const socket = new WebSocketClient({ userId: '', token: '' });

socket.publish({
    eventName: 'from:example',
    data: { body: 'example message' }
})

socket.subscribe('example', (message) => {
    console.log('Recibed: ', message);
})

if (socket.status() === SOCKET_STATUS.DISCONNECTED) console.log('Socket disconnected!')