{"_id":"@chancecox/websocket-client","name":"@chancecox/websocket-client","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@chancecox/websocket-client","version":"1.0.0","description":"A comprehensive WebSocket client package with SimpleWebSocket (app authentication) and WebSocketClient (advanced features)","type":"module","main":"dist/websocket-client.js","module":"dist/websocket-client.esm.js","scripts":{"build":"rollup -c","dev":"rollup -c -w","test":"jest","test:watch":"jest --watch","test:simple":"jest tests/SimpleWebSocket.test.js","test:advanced":"jest tests/WebSocketClient.test.js","lint":"eslint src/**/*.js","lint:fix":"eslint src/**/*.js --fix","docs":"jsdoc src -d docs","prepublishOnly":"npm run build && npm run test:simple"},"keywords":["websocket","client","authentication","app-id","app-secret","reconnection","events","ping","pong","real-time","communication","laravel","bref","aws"],"author":{"name":"Chance Cox","email":"chance.cox@gmail.com"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/chancemcox/websocket-client-npm.git"},"bugs":{"url":"https://github.com/chancecox/websocket-client-npm/issues"},"homepage":"https://github.com/chancecox/websocket-client-npm#readme","devDependencies":{"@babel/core":"^7.22.0","@babel/preset-env":"^7.22.0","@rollup/plugin-babel":"^6.0.0","@rollup/plugin-commonjs":"^25.0.0","@rollup/plugin-node-resolve":"^15.0.0","@rollup/plugin-terser":"^0.4.4","core-js":"^3.45.0","eslint":"^8.40.0","jest":"^29.5.0","jest-environment-jsdom":"^30.0.5","jsdoc":"^4.0.0","rollup":"^3.20.0","rollup-plugin-dts":"^5.0.0","rollup-plugin-typescript2":"^0.35.0","typescript":"^5.0.0"},"peerDependencies":{"reconnecting-websocket":"^4.4.0"},"engines":{"node":">=14.0.0"},"_id":"@chancecox/websocket-client@1.0.0","gitHead":"31935551801ac36ee35353dfa544268f076261bc","_nodeVersion":"20.5.0","_npmVersion":"9.8.0","dist":{"integrity":"sha512-ii1AanKC1w2AJr8+Ws5cCJG2rcoYMhZueElOl/EbykUCefxnmUFIlp1OrT7EcRbWaqISZzcGxlvglFnNc99IZQ==","shasum":"a148a8123ec2a92a48706a1b51b4853a3afebce1","tarball":"https://registry.npmjs.org/@chancecox/websocket-client/-/websocket-client-1.0.0.tgz","fileCount":11,"unpackedSize":1140599,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDoSrD8oHi9XhQGZ4kc6WUIhE6SFCSLEbXZCLOkDCF7GQIgWtSIcc3Ai+FhHUPWQGaV4yOym6IFWXsUzK9ajoA4myE="}]},"_npmUser":{"name":"chancecox","email":"chance.cox@gmail.com"},"directories":{},"maintainers":[{"name":"chancecox","email":"chance.cox@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/websocket-client_1.0.0_1755106128650_0.6857090761667748"},"_hasShrinkwrap":false}},"time":{"created":"2025-08-13T17:28:48.536Z","1.0.0":"2025-08-13T17:28:49.004Z","modified":"2025-08-13T17:28:49.311Z"},"maintainers":[{"name":"chancecox","email":"chance.cox@gmail.com"}],"description":"A comprehensive WebSocket client package with SimpleWebSocket (app authentication) and WebSocketClient (advanced features)","homepage":"https://github.com/chancecox/websocket-client-npm#readme","keywords":["websocket","client","authentication","app-id","app-secret","reconnection","events","ping","pong","real-time","communication","laravel","bref","aws"],"repository":{"type":"git","url":"git+https://github.com/chancemcox/websocket-client-npm.git"},"author":{"name":"Chance Cox","email":"chance.cox@gmail.com"},"bugs":{"url":"https://github.com/chancecox/websocket-client-npm/issues"},"license":"MIT","readme":"# websocket-client-io\n\nA modern, feature-rich WebSocket client with event handling, reconnection, and ping/pong support. Built on top of `reconnecting-websocket` for resilient connections and a simple, event-driven API.\n\n## Installation\n\n```bash\nnpm install websocket-client-io reconnecting-websocket\n```\n\n## Quick start\n\n```js\nimport { createWebSocketClient } from 'websocket-client-io';\n\nconst client = createWebSocketClient('wss://example.com/socket', {\n  pingTime: 20, // seconds\n  debug: true,\n});\n\nclient.on('open', () => {\n  console.log('Connected');\n});\n\nclient.on('message', ({ event, data }) => {\n  console.log('Message:', event, data);\n});\n\nclient.emit('message', { hello: 'world' });\n\n// Listen for a specific action (server-side event name)\nclient.listen('chatMessage', (data) => {\n  console.log('Chat:', data);\n});\n```\n\n## API\n\n### createWebSocketClient(url, options?)\nCreates and returns a `WebSocketClient` that automatically reconnects.\n\n- **url**: WebSocket URL (`wss://` or `ws://`).\n- **options**: Object\n  - **pingTime**: number (default: 15) — seconds between automatic pings\n  - **reconnectingOptions**: options passed to `reconnecting-websocket` (defaults shown below)\n  - Any other option is forwarded to `WebSocketClient` (see below)\n\nDefault `reconnectingOptions` used if not provided:\n```js\n{\n  reconnectInterval: 1000,\n  timeoutInterval: 10000,\n  maxReconnectAttempts: 10,\n}\n```\n\nReturns: `WebSocketClient`\n\n### Class: WebSocketClient\n\nConstructs a client around an existing `WebSocket` or `ReconnectingWebSocket` instance, or a URL string.\n\nConstructor options:\n- **pingInterval**: number (default: 15) — seconds between automatic pings\n- **autoPing**: boolean (default: true) — whether to automatically send pings\n- **debug**: boolean (default: false) — log to console when true\n\nKey methods:\n- **on(event, callback, options?)**: Subscribe to an event (see Events below)\n- **off(event, callback)**: Unsubscribe\n- **listen(action, callback)**: Subscribe to a specific message action or `'*'` for all\n- **emit(action, data?)**: Send `{ action, data }`\n- **chatMessage(data?)**: Send `{ action: 'chatMessage', body: data }`\n- **message(data?)**: Send `{ action: 'message', body: data }`\n- **ping()**: Send a raw `Ping`\n- **close()**: Close the socket and stop auto-ping\n- **markAsClosed()**: Mark connection as closed and stop auto-ping\n- **onOpen(callback)**: Run once when connection is open (immediately if already open)\n- **getStatus()**: Returns `{ open, readyState, url, connectionId, messageQueueLength, pingInterval, autoPing }`\n- **setConnectionId(id)**: Tag the connection\n- **destroy()**: Cleanup listeners and close the connection\n\n## Events\n\nThe client dispatches DOM `CustomEvent`s internally and exposes a simple `on()` API.\n\nBuilt-in events:\n- **open**: connection established\n- **close**: connection closed\n- **error**: error raised by the socket\n- **message**: all messages, with shape `{ event, data, original }`\n\nServer-defined events:\n- Any `event`/`action` property in server messages is dispatched with that name.\n- Example: a server message `{ event: 'chatMessage', data: {...} }` triggers `on('chatMessage', handler)`.\n\n## Message formats\n\nIncoming handling:\n- If the raw message is the string `'Pong'` or empty string, the client treats it as a pong.\n- Otherwise, it attempts `JSON.parse(event.data)` and uses `data.data || data.body || data` as the payload.\n\nOutgoing helpers:\n- `emit(action, data?)` sends `{ action, data }` (when `data` is provided)\n- `message(data?)` sends `{ action: 'message', body: data }`\n- `chatMessage(data?)` sends `{ action: 'chatMessage', body: data }`\n\n## Usage notes\n\n- This library targets browser environments (uses `document` and `CustomEvent`). For Node-based tests, use a DOM shim like `jsdom`.\n- If you pass a native `WebSocket`, it is wrapped in a `ReconnectingWebSocket` for resilience.\n- Set `debug: true` to see prefixed logs in the console.\n\n## Generating documentation\n\nJSDoc comments are included throughout the source. Generate static docs with:\n\n```bash\nnpm run docs\n```\n\nThis will output HTML docs to the `docs/` directory.\n\n## Deprecations\n\n- `webSocketIOconnect(url, pingTime?)` is deprecated. Use `createWebSocketClient(url, { pingTime })` instead.\n\n## License\n\nMIT","readmeFilename":"README.md","_rev":"1-e6504fbfbe8aeb241f3a8bbe66d6e42a"}