{"id":18144805,"url":"https://github.com/iamlizu/sockmanage","last_synced_at":"2025-04-23T09:12:47.798Z","repository":{"id":260107849,"uuid":"880205049","full_name":"IamLizu/sockmanage","owner":"IamLizu","description":"🔌 Manage single active WebSocket connections per user with Redis-powered persistence.","archived":false,"fork":false,"pushed_at":"2024-10-30T06:35:59.000Z","size":113,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-23T09:12:32.574Z","etag":null,"topics":["redis","socket-io","websockets"],"latest_commit_sha":null,"homepage":"https://yarnpkg.com/package?name=sockmanage","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/IamLizu.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-29T10:07:44.000Z","updated_at":"2025-01-12T18:29:14.000Z","dependencies_parsed_at":"2024-10-29T15:35:18.810Z","dependency_job_id":null,"html_url":"https://github.com/IamLizu/sockmanage","commit_stats":null,"previous_names":["iamlizu/sockmanage"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IamLizu%2Fsockmanage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IamLizu%2Fsockmanage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IamLizu%2Fsockmanage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IamLizu%2Fsockmanage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IamLizu","download_url":"https://codeload.github.com/IamLizu/sockmanage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250403833,"owners_count":21424795,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["redis","socket-io","websockets"],"created_at":"2024-11-01T20:06:15.104Z","updated_at":"2025-04-23T09:12:47.781Z","avatar_url":"https://github.com/IamLizu.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SockManage\n\nSockManage is a utility library for managing WebSocket connections, ensuring each user has a single active socket connection at any given time. It uses Redis for persistence, making it suitable for scalable and distributed WebSocket applications.\n\n## Features\n\n-   Ensures one active socket per user.\n-   Manages user sockets with Redis for persistence across multiple instances.\n-   Provides methods to register, deregister, and retrieve user sockets.\n-   Supports emitting events to specific users.\n\n## Installation\n\nTo install with npm:\n\n```\nnpm install sockmanage\n```\n\nor with Yarn:\n\n```\nyarn add sockmanage\n```\n\n## Usage\n\n### Importing and Setting Up\n\n```typescript\nimport { createClient } from \"redis\";\nimport { Server as SocketIOServer } from \"socket.io\";\nimport { SockManage } from \"sockmanage\";\n\n// Initialize Redis and Socket.IO\nconst redisClient = createClient();\nconst io = new SocketIOServer(server); // assume 'server' is an HTTP server\n\n// Initialize SockManage\nconst socketManager = new SockManage({ redis: redisClient });\n\n// Set up the Socket.IO server and specify the namespace (optional)\nsocketManager.setup({ io, namespace: \"/your-namespace\" });\n\n// Assuming top-level await is supported, otherwise wrap the following line in an async function\nawait socketManager.initialize();\n```\n\n### Methods\n\n#### `setup`\n\nSets up Socket.IO server and optional namespace.\n\n```typescript\nsocketManager.setup({ io, namespace: \"/your-namespace\" });\n```\n\n**Parameters:**\n\n-   `io`: Instance of `SocketIOServer`.\n-   `namespace` (optional): Namespace for Socket.IO events.\n\n#### `initialize`\n\nInitializes user sockets from Redis.\n\n```typescript\nawait socketManager.initialize();\n```\n\n#### `getSockets`\n\nRetrieves all user sockets from local map.\n\n```typescript\nconst userSockets = socketManager.getSockets();\n```\n\n**Returns:** `Map\u003cstring, string\u003e`: A map of user IDs to socket IDs, or an empty map.\n\n#### `getSocket`\n\nRetrieves the socket ID for a specific user.\n\n```typescript\nconst socketId = socketManager.getSocket(\"userId\");\n```\n\n**Parameters:**\n\n-   `userId`: ID of the user.\n\n**Returns:** `string | null`: Socket ID of the user or `null` if not found.\n\n#### `register`\n\nRegisters a socket for a user and ensures only one active socket per user.  \n**Note:** The `data` parameter must be a JSON string containing the `userId`.\n\n```typescript\nawait socketManager.register(socket, JSON.stringify({ userId: \"user1\" }));\n```\n\n**Parameters:**\n\n-   `socket`: The socket instance.\n-   `data`: A JSON string containing the `userId`.\n\n#### `deRegister`\n\nDe-registers a socket for a user.\n\n```typescript\nsocketManager.deRegister(socket);\n```\n\n**Parameters:**\n\n-   `socket`: The socket instance to deregister.\n\n#### `inform`\n\nEmits an event to a specific socket.\n\n```typescript\nsocketManager.inform({\n    socketId: \"socket1\",\n    _event: \"message\",\n    data: { message: \"Hello, User!\" },\n});\n```\n\n**Parameters:**\n\n-   `socketId`: ID of the socket to emit the event to.\n-   `_event`: Event name.\n-   `data`: Data to send with the event.\n\n## Example\n\n```typescript\nimport { createClient } from \"redis\";\nimport { Server as SocketIOServer } from \"socket.io\";\nimport { SockManage } from \"sockmanage\";\n\nconst redisClient = createClient();\nconst io = new SocketIOServer(server);\n\nconst socketManager = new SockManage({ redis: redisClient });\nsocketManager.setup({ io });\n\n// Assuming top-level await is supported, otherwise wrap the following line in an async function\nawait socketManager.initialize();\n\nio.on(\"connection\", (socket) =\u003e {\n    // You be getting the userId from anywhere, it doesn't matter where you get it from\n    // as long as you pass it to the register method.\n    const userId = socket.handshake.query.userId;\n\n    socketManager.register(socket, JSON.stringify({ userId }));\n\n    socket.on(\"disconnect\", () =\u003e {\n        socketManager.deRegister(socket);\n    });\n\n    // this following block is completely optional, you shall proceed with using your own event sending logic\n    socket.on(\"message\", (data) =\u003e {\n        socketManager.inform({\n            socketId: socket.id,\n            _event: \"message\",\n            data: { message: \"Hello, User!\" },\n        });\n    });\n});\n```\n\n## Testing\n\nTo run the tests, use the following command:\n\n```bash\nyarn test\n```\n\n## Changelog\n\nDetailed changes for each version are documented in the [History.md](History.md) file.\n\n## License\n\nMIT License. See [LICENSE](LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamlizu%2Fsockmanage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamlizu%2Fsockmanage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamlizu%2Fsockmanage/lists"}