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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tapsioss/client-socket-manager

v0.7.0

Published

<div align="center">

Downloads

60

Readme

Client Socket Manager

A wrapper for socket.io-client that handles best practices and edge cases in a more abstracted and opinionated manner.


ClientSocketManager is a flexible and robust manager for handling socket connections using socket.io-client. It provides easy setup and management of socket connections with support for automatic reconnections, event handling, and visibility change management.

Installation

First, install the necessary dependencies:

npm install @tapsioss/client-socket-manager socket.io-client

Usage

Here is an example of how to use ClientSocketManager in your project:

import { ClientSocketManager } from "@tapsioss/client-socket-manager";

const socketManager = new ClientSocketManager("http://localhost:3000", {
  eventHandlers: {
    onSocketConnection() {
      console.log("Socket connected");
    },
    onSocketDisconnection(reason) {
      console.log("Socket disconnected:", reason);
    },
    onSuccessfulReconnection(attempt) {
      console.log("Socket reconnected after", attempt, "attempt(s)");
    },
    onAnySubscribedMessageReceived(channel, message) {
      console.log(`Message received on ${channel}:`, message);
    },
  },
});

// Emit an event
socketManager.emit("message", "Hello, world!");

// Subscribe to a channel
socketManager.subscribe("message", msg => {
  console.log("Message from server:", msg);
});

API Reference

Constructor

constructor(uri: string, options?: ClientSocketManagerOptions)

Parameters

  • uri: The URI of the socket server.
  • options: (optional): Configuration options for the socket connection.
Options

We have extended socket-io's options to include additional options:

  • eventHandlers: Handlers for various socket events.
    • onInit: Fired upon instantiation.
    • onDispose: Fired upon disposal.
    • onSocketConnection: Fired when the socket is successfully connected.
    • onSocketDisconnection: Fired when the socket is disconnected.
    • onServerPing: Fired when a ping packet is received from the server.
    • onConnectionError: Fired upon a connection error.
    • onReconnecting: Fired upon an attempt to reconnect.
    • onReconnectingError: Fired upon a reconnection attempt error.
    • onReconnectionFailure: Fired when couldn't reconnect within reconnectionAttempts.
    • onSuccessfulReconnection: Fired upon a successful reconnection.
    • onAnySubscribedMessageReceived: Fired when any message is received from a subscribed channel.
    • onVisiblePage: Fired when the page's visibilityState changes to visible.
    • onHiddenPage: Fired when the page's visibilityState changes to hidden.
  • devtool: Enables the in-browser DevTool panel for socket debugging. This is useful for development and debugging purposes. In production environments, it's recommended to leave this disabled.

Properties

id: string | null

A unique identifier for the session. null when the socket is not connected.

connected: boolean

Whether the socket is currently connected to the server.

disposed: boolean

Whether the client is disposed.

recovered: boolean

Whether the connection state was recovered after a temporary disconnection.

autoReconnectable: boolean

Whether the Socket will try to reconnect when its Manager connects or reconnects.

Methods

emit

emit<Ev extends EventNames<EmitEvents>>(
  channel: Ev,
  ...args: EventParams<EmitEvents, Ev>
): void;

Emits an event to the socket identified by the channel name.

Parameters
  • channel: The name of the channel to emit the event to.
  • args: The arguments to pass with the event.

subscribe

subscribe<Ev extends EventNames<ListenEvents>>(
  channel: Ev,
  cb: ListenEvents[Ev],
  options?: {
    onSubscriptionComplete?: (channel: string) => void;
    signal?: AbortSignal;
  },
): void;

Subscribes to a specified channel with a callback function. Ensures that only one listener exists per channel.

Parameters
  • channel: The name of the channel to subscribe to.
  • cb: The callback function to invoke when a message is received on the channel.
  • options: Optional parameters.
    • onSubscriptionComplete: The callback function to invoke when the subscription is complete.
    • signal: The AbortSignal to unsubscribe the listener upon abortion.

unsubscribe

unsubscribe<Ev extends EventNames<ListenEvents>>(
  channel: Ev,
  cb?: ListenEvents[Ev],
): void;

Removes the listener for the specified channel. If no callback is provided, it removes all listeners for that channel.

Parameters
  • channel: The name of the channel whose listener should be deleted.
  • cb (optional): The subscriber callback function to remove.

connect

connect(): void;

Manually connects/reconnects the socket.

disconnect

disconnect(): void;

Manually disconnects the socket. In that case, the socket will not try to reconnect. If this is the last active Socket instance of the Manager, the low-level connection will be closed.

dispose

dispose(): void;

Disposes of the socket, manager, and engine, ensuring all connections are closed and cleaned up.

showDevtool

showDevtool(options): void;

Show devtool in the browser programmatically.

Parameters
  • options: Optional parameters.
    • zIndex: Z-index of the devtool, overrides the previous z-index of the devtool.

hideDevtool

hideDevtool(): void;

Hide devtool in the browser programmatically.

ClientSocketManagerStub

The package also exports a stubbed version of the socket manager for use in testing or server-side rendering (SSR) environments:

import { ClientSocketManagerStub } from "@tapsioss/client-socket-manager";

const stub = new ClientSocketManagerStub("mock://url", {
  eventHandlers: {
    onSocketConnection() {
      console.log("Simulated connection");
    },
  },
});

stub.connect(); // Triggers onSocketConnection
stub.emit("message", "noop"); // No-op
stub.dispose(); // Marks the client as disposed

Why use the stub?

  • Prevents actual network communication in unit tests and SSR.
  • Mimics the API surface of the real ClientSocketManager.
  • Triggers configured event handlers like onSocketConnection and onSocketDisconnection.

Stub Behavior Summary

  • Methods like emit, subscribe, and unsubscribe are no-ops.
  • connect() and disconnect() simulate connection lifecycle events.
  • The connected, disposed, id, and other properties behave consistently with a mock socket.

License

This project is licensed under the terms of the MIT license.