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

socket-auth-room-manager

v1.0.1

Published

Socket.IO authentication middleware and room management utility with JWT support

Readme

🚀 socket-auth-room-manager

A lightweight and flexible Socket.IO authentication + room management utility with JWT support.

Simplifies:

  • 🔐 Socket authentication (JWT + cookies)
  • 🏠 Room join/leave management
  • 👥 User tracking inside rooms
  • ⚡ Emitting events to rooms
  • 🧼 Automatic cleanup on disconnect

📦 Installation

npm install socket-auth-room-manager

⚡ Quick Start

import { Server } from "socket.io";
import {
  socketAuth,
  joinRoom,
  emitToRoom,
  setupDisconnectHandler,
} from "socket-auth-room-manager";

const io = new Server(server, {
  cors: { origin: "*" },
});

// 🔐 Attach authentication middleware
io.use(socketAuth({ secret: "your_jwt_secret" }));

io.on("connection", (socket) => {
  console.log("User connected:", socket.user);

  // 🧼 Handle disconnect cleanup
  setupDisconnectHandler(socket);

  // 🏠 Join a room
  joinRoom(socket, "room1");

  // 📡 Emit messages to room
  socket.on("message", (msg) => {
    emitToRoom(io, "room1", "message", msg);
  });
});

🔐 Authentication

Supports token from:

  • socket.handshake.auth.token
  • httpOnly cookies

Example (client)

const socket = io("http://localhost:3000", {
  auth: {
    token: "your_jwt_token",
  },
});

🧠 API Reference


🔐 socketAuth(options)

Middleware for authenticating socket connections.

Options:

  • secret (required): JWT secret key
  • cookieName (optional): default "token"

Usage:

io.use(socketAuth({ secret: "your_secret" }));

🏠 joinRoom(socket, roomId)

Adds a user to a room.

joinRoom(socket, "room1");

🚪 leaveRoom(socket, roomId)

Removes a user from a room.

leaveRoom(socket, "room1");

📡 emitToRoom(io, roomId, event, data)

Broadcasts an event to all users in a room.

emitToRoom(io, "room1", "message", { text: "Hello" });

👥 getRoomUsers(roomId)

Returns users inside a room.

const users = getRoomUsers("room1");

📊 getRoomSize(roomId)

Returns number of users in a room.

const size = getRoomSize("room1");

🗂️ getAllRooms()

Returns all active rooms.

const rooms = getAllRooms();

🧼 setupDisconnectHandler(socket)

Automatically removes user from all rooms on disconnect.

setupDisconnectHandler(socket);

🧩 Features

  • ✅ JWT-based authentication
  • 🍪 Cookie support (httpOnly compatible)
  • ⚡ Lightweight and fast (in-memory store)
  • 🧠 User tracking per room
  • 🔄 Automatic cleanup on disconnect
  • 🧼 Clean and minimal API design

⚠️ Notes

  • This package uses in-memory storage → Not suitable for multi-server scaling (use Redis for that)

  • Designed for simplicity and performance


🚀 Future Improvements

  • Redis adapter support
  • Role-based rooms
  • Rate limiting per room
  • Event logging hooks

🤝 Contributing

Contributions are welcome!

  1. Fork the repo
  2. Create a new branch
  3. Commit changes
  4. Open a pull request

📄 License

MIT © Sarvesh Wani


⭐ Support

If you find this useful, consider giving it a ⭐ on GitHub!