{"id":22542005,"url":"https://github.com/t34-dev/ts-ws-client","last_synced_at":"2026-01-07T22:04:23.829Z","repository":{"id":251043495,"uuid":"836152627","full_name":"t34-dev/ts-ws-client","owner":"t34-dev","description":"Robust TypeScript WebSocket client with auto-reconnection for React, Node.js, and web projects. Supports event handling and connection management.","archived":false,"fork":false,"pushed_at":"2024-08-05T14:28:29.000Z","size":150,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-20T03:03:55.777Z","etag":null,"topics":["node","package","react","starter","ts","typescript","web"],"latest_commit_sha":null,"homepage":"https://t34-dev.github.io/ts-ws-client/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/t34-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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-07-31T08:59:49.000Z","updated_at":"2024-08-05T14:29:17.000Z","dependencies_parsed_at":"2024-08-02T10:43:27.516Z","dependency_job_id":"04e05f42-f7c0-49c4-b7e7-30769ba0c1f0","html_url":"https://github.com/t34-dev/ts-ws-client","commit_stats":null,"previous_names":["well-do-it-too/ts-websocket-client","t34-dev/ts-ws-client","t34-developer/ts-ws-client"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fts-ws-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fts-ws-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fts-ws-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fts-ws-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/t34-dev","download_url":"https://codeload.github.com/t34-dev/ts-ws-client/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246049628,"owners_count":20715510,"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":["node","package","react","starter","ts","typescript","web"],"created_at":"2024-12-07T13:08:12.939Z","updated_at":"2026-01-07T22:04:18.810Z","avatar_url":"https://github.com/t34-dev.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)\n![TypeScript](https://img.shields.io/badge/TypeScript-5.5.3-blue?logo=typescript\u0026ver=1722868108)\n![Node.js](https://img.shields.io/badge/Node.js-%3E%3D20-green?logo=node.js\u0026ver=1722868108)\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/t34-dev/ts-ws-client?ver=1722868108)\n![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/t34-dev/ts-ws-client?sort=semver\u0026style=flat\u0026logo=git\u0026logoColor=white\u0026label=Latest%20Version\u0026color=blue\u0026ver=1722868108)\n\n# TypeScript WebSocket Client\n\nA robust and flexible WebSocket client implementation in TypeScript, designed for use in React, Node.js, and web projects.\nBased on https://github.com/t34-dev/ts-universal-package\n\n![TypeScript WebSocket Client Logo](./assets/example.png)\n\n- [Demo](https://t34-dev.github.io/ts-ws-client/)\n\n## Features\n\n- Easy-to-use WebSocket client implementation\n- Automatic reconnection with exponential backoff\n- Event-based architecture for easy integration\n- TypeScript support for better developer experience\n- Compatible with React, Node.js, and web projects\n- Customizable logging and debugging options\n\n## Installation\n\n```bash\nnpm install @t34-dev/ts-ws-client\n```\n\nor\n\n```bash\nyarn add @t34-dev/ts-ws-client\n```\n\n## Usage\n\n### Basic Example\n\nHere's a basic example of how to use the WebSocket client:\n\n```typescript\nimport { WebSocketClient } from '@t34-dev/ts-ws-client';\n\nconst client = new WebSocketClient({\n  connectionName: 'MyConnection',\n  url: 'wss://echo.websocket.org',\n  debugging: true,\n  onOpened: (info) =\u003e console.log('Connected:', info),\n  onClosed: (info) =\u003e console.log('Disconnected:', info),\n  onError: (info) =\u003e console.error('Error:', info),\n  onUpdate: (info) =\u003e console.log('Received:', info.data),\n});\n\nclient.open();\n\n// Send a message\nclient.sendMessage('Hello, WebSocket!');\n\n// Close the connection\nclient.close();\n```\n\n### Node.js Example\n\nHere's an example of how to use the WebSocket client in a Node.js environment:\n\n```typescript\nimport { WebSocketClient } from '@t34-dev/ts-ws-client';\n\nconst client = new WebSocketClient({\n  connectionName: 'NodeConnection',\n  url: 'wss://echo.websocket.org',\n  debugging: true,\n  onOpened: (info) =\u003e {\n    console.log('Connected:', info);\n    // Send a message every 5 seconds\n    setInterval(() =\u003e {\n      client.sendMessage('Ping from Node.js');\n    }, 5000);\n  },\n  onClosed: (info) =\u003e console.log('Disconnected:', info),\n  onError: (info) =\u003e console.error('Error:', info),\n  onUpdate: (info) =\u003e console.log('Received:', info.data),\n});\n\nclient.open();\n\n// Handle process termination\nprocess.on('SIGINT', () =\u003e {\n  console.log('Closing connection...');\n  client.close();\n  process.exit();\n});\n```\n\n### React Example\n\nHere's an example of how to use the WebSocket client in a React component:\n\n```tsx\nimport React, { useEffect, useState } from 'react';\nimport { WebSocketClient } from '@t34-dev/ts-ws-client';\n\nconst WebSocketComponent: React.FC = () =\u003e {\n  const [client, setClient] = useState\u003cWebSocketClient | null\u003e(null);\n  const [messages, setMessages] = useState\u003cstring[]\u003e([]);\n\n  useEffect(() =\u003e {\n    const wsClient = new WebSocketClient({\n      connectionName: 'ReactConnection',\n      url: 'wss://echo.websocket.org',\n      debugging: true,\n      onOpened: (info) =\u003e console.log('Connected:', info),\n      onClosed: (info) =\u003e console.log('Disconnected:', info),\n      onError: (info) =\u003e console.error('Error:', info),\n      onUpdate: (info) =\u003e {\n        setMessages((prevMessages) =\u003e [...prevMessages, info.data as string]);\n      },\n    });\n\n    setClient(wsClient);\n    wsClient.open();\n\n    return () =\u003e {\n      wsClient.close();\n    };\n  }, []);\n\n  const sendMessage = () =\u003e {\n    if (client) {\n      client.sendMessage('Hello from React!');\n    }\n  };\n\n  return (\n    \u003cdiv\u003e\n      \u003cbutton onClick={sendMessage}\u003eSend Message\u003c/button\u003e\n      \u003cul\u003e\n        {messages.map((message, index) =\u003e (\n          \u003cli key={index}\u003e{message}\u003c/li\u003e\n        ))}\n      \u003c/ul\u003e\n    \u003c/div\u003e\n  );\n};\n\nexport default WebSocketComponent;\n```\n\n## API\n\n### `WebSocketClient`\n\nThe main class for creating and managing WebSocket connections.\n\n#### Constructor\n\n```typescript\nnew WebSocketClient(options: WebSocketOptions)\n```\n\n- `options`: Configuration options for the WebSocket client\n\n#### Methods\n\n- `open()`: Opens the WebSocket connection\n- `close()`: Closes the WebSocket connection\n- `breakConnection()`: Forcibly breaks the connection\n- `sendMessage(data: string)`: Sends a message through the WebSocket\n- `subscribe(data: string)`: Subscribes to a topic (stores the request)\n- `unsubscribe(data: string)`: Unsubscribes from a topic\n- `getInfo()`: Returns current connection information\n- `getStoredRequests()`: Returns the set of stored requests\n- `clearStoredRequests()`: Clears all stored requests\n\n## Development\n\nTo set up the development environment:\n\n1. Clone the repository\n2. Install dependencies: `npm install` or `yarn install`\n3. Run tests: `npm test` or `yarn test`\n4. Build the package: `npm run build` or `yarn build`\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the ISC License.\n\n## Links\n\n- [GitHub Repository](https://github.com/t34-dev/ts-ws-client)\n- [npm Package](https://www.npmjs.com/package/@t34-dev/ts-ws-client)\n- [Demo](https://t34-dev.github.io/ts-ws-client/)\n\n---\n\nDeveloped with ❤️ by [T34](https://github.com/t34-dev)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft34-dev%2Fts-ws-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ft34-dev%2Fts-ws-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft34-dev%2Fts-ws-client/lists"}