DEV Community

S M Mahmudul Hasan
S M Mahmudul Hasan

Posted on

SockManage — 🔌 Manage single active WebSocket connections per user with Redis-powered persistence

Hey Dev.to community! 👋

I’m excited to share a project I’ve been working on called SockManage—a Node.js library designed to simplify WebSocket connection management, with Redis integration to handle distributed server setups. Whether you’re building real-time apps, chat applications, or scalable WebSocket-based systems, sockmanage aims to provide reliable, straightforward socket management.

🌟 What is SockManage?

SockManage is a lightweight library focused on managing WebSocket connections, ensuring that each user has either a single or multiple active connection(s) as needed. It uses Redis for persistence, making it highly suitable for applications running across multiple servers.

I’d love to hear your thoughts on this project and get feedback on both its design and usability!

🔧 Key Features

  • Single Active Connection per User (with upcoming support for multiple connections): Allows you to maintain only one connection per user, automatically disconnecting any previous connections.
  • Redis-Powered Persistence: Stores and retrieves active user sockets in Redis, ensuring a consistent experience across distributed servers.
  • User Socket Management:
    • Register and deregister sockets for users.
    • Retrieve active sockets for specific users.
  • Event Emission: Easily emit events to individual user sockets.
  • Namespace Support: Define namespaces for organized WebSocket management.

🚀 Getting Started

Install sockmanage with npm or yarn:

npm install sockmanage
# or
yarn add sockmanage
Enter fullscreen mode Exit fullscreen mode
Basic Setup

Let’s walk through a basic setup example.

  1. Initialize Redis and Socket.IO:
   import { createClient } from "redis";
   import { Server as SocketIOServer } from "socket.io";
   import { SockManage } from "sockmanage";

   const redisClient = createClient();
   const io = new SocketIOServer(server); // Assume 'server' is an HTTP server
   const socketManager = new SockManage({ redis: redisClient });

   socketManager.setup({ io, namespace: "/chat" });

   // Initialize user sockets from Redis
   await socketManager.initialize();
Enter fullscreen mode Exit fullscreen mode
  1. Register Connections:
   io.on("connection", (socket) => {
       const userId = socket.handshake.query.userId;
       socketManager.register(socket, JSON.stringify({ userId }));

       socket.on("disconnect", () => {
           socketManager.deRegister(socket);
       });

       socket.on("message", (data) => {
           socketManager.inform({
               socketId: socket.id,
               _event: "message",
               data: { message: "Hello, User!" },
           });
       });
   });
Enter fullscreen mode Exit fullscreen mode

📦 What’s New in v1.0.2?

Recently, I made some changes to improve SockManage’s usability and clarity:

  • Shortened Method Names: Public method names are now simplified (e.g., registerSocketForUser is now register).
  • Deprecated Warnings for Legacy Methods: Old method names are available with a deprecation warning to maintain backward compatibility.

I’m currently planning the v1.1.0 release, which will introduce single and multiple connection modes, offering more flexibility for applications requiring different connection strategies.

🧐 I’d Love Your Feedback!

I’m looking for feedback from fellow developers and WebSocket users. Here are some things I’d love to get your input on:

  • Functionality: Are there other features you think sockmanage should include?
  • Usability: How intuitive is the API? Any suggestions on making it simpler?
  • Reliability: If you try it out, I’d love to know if you encounter any issues or have ideas for improvement.

🌐 Project Links

Thank you for reading, and a big thanks in advance for any feedback you share! 🙌

Top comments (0)