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

@tsdiapi/socket.io

v0.4.0

Published

WebSocket plugin for the TSDIAPI-Server framework, providing seamless integration with Socket.IO and socket-controllers.

Readme

TSDIAPI-Socket.IO: WebSocket Plugin for TSDIAPI-Server

The TSDIAPI-Socket.IO plugin integrates WebSocket functionality into TSDIAPI-Server, utilizing Socket.IO for real-time communication.
This plugin provides a flexible and functional approach, replacing the previous declarative model. It supports authenticated and unauthenticated connections, with enhanced event handling and session management.


🚀 Features

Functional WebSocket Integration – No decorators, just clear and structured functions.
Authentication Support – Validate and manage authenticated socket connections.
Customizable Event Handling – Define custom handlers for incoming messages.
Type-Safe Socket Communication – Strongly typed payloads and responses.
Automatic WebSocket Lifecycle Management – Manages connections, authentication, and disconnections.


📦 Installation

Install the plugin via npm:

npm install @tsdiapi/socket.io

Or use the TSDIAPI CLI to install and configure it automatically:

tsdiapi plugins add socket.io

📂 Code Generation

| Command | Description | |--------------------------|-----------------------------------------------| | tsdiapi generate socket.io | Creates a new WebSocket event handler template. |

The tsdiapi generate socket.io command generates a basic WebSocket handler, allowing for quick implementation of custom socket logic.


🛠 Getting Started

1️⃣ Register the Plugin in Your Application

To enable WebSocket functionality, register the plugin in your TSDIAPI-Server application:

import { createApp } from "@tsdiapi/server";
import createSocketIOPlugin from "@tsdiapi/socket.io";

createApp({
  plugins: [createSocketIOPlugin()],
});

2️⃣ Handling WebSocket Connections

Define WebSocket events and implement custom handlers within your server:

import { FastifyInstance } from "fastify";

export function setupWebSocketHandlers(io: FastifyInstance["io"]) {
  io.on("connection", (socket) => {
    console.log("Client connected:", socket.id);

    socket.on("message", (data) => {
      console.log("Received message:", data);
      socket.emit("response", { status: "ok", message: "Message received!" });
    });

    socket.on("disconnect", () => {
      console.log("Client disconnected:", socket.id);
    });
  });
}

This functional approach makes it easier to customize and extend WebSocket logic without relying on decorators.


3️⃣ Supporting Authentication

TSDIAPI-Socket.IO supports authentication through the verify function:

import createSocketIOPlugin from "@tsdiapi/socket.io";

createSocketIOPlugin({
  verify: async (token: string) => {
    // Simulate token validation
    if (token === "valid-token") {
      return { userId: "12345", role: "admin" }; // Example session object
    }
    throw new Error("Authentication failed");
  },
});
  • If verify is provided, the server rejects unauthenticated connections.
  • The session object is attached to the socket for later use.

Example handling authenticated sockets:

io.on("connection", (socket) => {
  if ("session" in socket) {
    console.log(`User ${socket.session.userId} connected.`);
  }
});

📖 API Reference

1️⃣ emitSuccess(event, data?)

Sends a success response to the client.

socket.emitSuccess("someEvent", { message: "Hello, world!" });

2️⃣ emitError(event, errors)

Sends an error response to the client.

socket.emitError("someEvent", "Something went wrong.");

3️⃣ on(event, listener)

Registers an event listener.

socket.on("message", (data) => {
  console.log("Received:", data);
});

🔌 Lifecycle Hooks

TSDIAPI-Socket.IO integrates into the TSDIAPI lifecycle:

| Hook | Description | |--------------|--------------------------------------------------| | onInit | Initializes the WebSocket server. | | beforeStart| Sets up authentication and event handling. |


📜 Logging & Debugging

TSDIAPI-Socket.IO logs key events for easier debugging:

[INFO] WebSocket server started on port 3000
[INFO] Client connected: abc123
[INFO] Received message: { text: "Hello" }
[INFO] Client disconnected: abc123
[ERROR] Authentication failed

📌 Example Full Implementation

import { createApp } from "@tsdiapi/server";
import createSocketIOPlugin from "@tsdiapi/socket.io";
import { setupWebSocketHandlers } from "./websocket-handlers";

const socketIOPlugin = createSocketIOPlugin({
  verify: async (token) => {
    if (token === "valid-token") return { userId: "123" };
    throw new Error("Invalid token");
  },
});

createApp({
  plugins: [socketIOPlugin],
});

🙌 Contributing

Contributions are welcome! 🎉

  • Report issues
  • Submit pull requests
  • Improve documentation

📜 License

Licensed under the MIT License. See the LICENSE file for details.


🚀 TSDIAPI-Socket.IO provides a fast, flexible, and fully customizable WebSocket integration.
Start building real-time applications today!