{"_id":"@bytemain/reconnecting-websocket","name":"@bytemain/reconnecting-websocket","dist-tags":{"latest":"4.4.0"},"versions":{"4.4.0":{"name":"@bytemain/reconnecting-websocket","version":"4.4.0","description":"Reconnecting WebSocket","main":"./dist/reconnecting-websocket-cjs.js","module":"./dist/reconnecting-websocket-mjs.js","types":"./dist/reconnecting-websocket.d.ts","scripts":{"build":"npm run clean && rollup -c && uglifyjs --compress --mangle -o dist/reconnecting-websocket-iife.min.js dist/reconnecting-websocket-iife.js","test":"jest --coverage","clean":"del dist && del coverage && del .nyc_output","report":"nyc report --reporter=html && opn coverage/index.html","coveralls":"cat ./coverage/lcov.info | ./node_modules/.bin/coveralls","lint":"DEBUG=eslint:file-enumerator eslint __tests__/*.* ./*.ts","prepublishOnly":"npm run clean && npm run lint && npm run build && npm test"},"keywords":["websocket","client","reconnecting","reconnection","reconnect","forever","persistent","forever","automatic"],"author":{"name":"bytemain","email":"artin@cat.ms"},"license":"MIT","devDependencies":{"@types/jest":"^25.1.2","@typescript-eslint/eslint-plugin":"^2.19.0","@typescript-eslint/parser":"^2.19.0","coveralls":"^3.0.9","del-cli":"^1.1.0","eslint":"^6.8.0","eslint-plugin-jest":"^23.7.0","husky":"^1.3.1","jest":"^25.1.0","lint-staged":"^7.3.0","opn-cli":"^3.1.0","prettier":"^1.19.1","rollup":"^0.66.6","rollup-plugin-typescript2":"^0.23.0","ts-jest":"^25.2.0","tslib":"^1.10.0","tslint":"^5.20.1","typescript":"^3.7.5","uglify-es":"^3.3.10","ws":"^6.2.1"},"dependencies":{},"directories":{"test":"test"},"repository":{"type":"git","url":"git+https://github.com/bytemain/reconnecting-websocket.git"},"bugs":{"url":"https://github.com/bytemain/reconnecting-websocket/issues"},"publishConfig":{"registry":"https://registry.npmjs.org","access":"public"},"homepage":"https://github.com/bytemain/reconnecting-websocket#readme","lint-staged":{"linters":{"*.{js,md,ts}":["prettier --write","git add"]}},"husky":{"hooks":{"pre-commit":"lint-staged"}},"_id":"@bytemain/reconnecting-websocket@4.4.0","gitHead":"3fc6df58820f5ca27dcb7d9a24c556a266a9e0dc","_nodeVersion":"20.12.2","_npmVersion":"10.5.0","dist":{"integrity":"sha512-UNhTAy6I3jbMW+LD5CZtlTRHP+m5yr2C9pL1bVoj2GYSqv/xJpJLmoFGkemGmV8FG8EfgOrRpuphOepnPC7uYg==","shasum":"def1850380098fd233ea1ef6d233920e02cddd30","tarball":"https://registry.npmjs.org/@bytemain/reconnecting-websocket/-/reconnecting-websocket-4.4.0.tgz","fileCount":11,"unpackedSize":141888,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCMcfEJ5OqmTKqkXpHFUUXhAxFJ7qP6ajw2D22KeEybDAIgbI1lxt2uqLFrpXgaRYhsN4EUBuy6MbexBcY8zMTS7fk="}]},"_npmUser":{"name":"lengthmin","email":"lengthmin@gmail.com"},"maintainers":[{"name":"lengthmin","email":"lengthmin@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/reconnecting-websocket_4.4.0_1715658058707_0.8332351136860063"},"_hasShrinkwrap":false}},"time":{"created":"2024-05-14T03:40:58.608Z","4.4.0":"2024-05-14T03:40:58.882Z","modified":"2024-05-14T03:40:59.185Z"},"maintainers":[{"name":"lengthmin","email":"lengthmin@gmail.com"}],"description":"Reconnecting WebSocket","homepage":"https://github.com/bytemain/reconnecting-websocket#readme","keywords":["websocket","client","reconnecting","reconnection","reconnect","forever","persistent","forever","automatic"],"repository":{"type":"git","url":"git+https://github.com/bytemain/reconnecting-websocket.git"},"author":{"name":"bytemain","email":"artin@cat.ms"},"bugs":{"url":"https://github.com/bytemain/reconnecting-websocket/issues"},"license":"MIT","readme":"# Reconnecting WebSocket\n\nWebSocket that will automatically reconnect if the connection is closed.\n\n## Features\n\n-   WebSocket API compatible (same interface, Level0 and Level2 event model)\n-   Fully configurable\n-   Multi-platform (Web, ServiceWorkers, Node.js, React Native)\n-   Dependency free (does not depend on Window, DOM or any EventEmitter library)\n-   Handle connection timeouts\n-   Allows changing server URL between reconnections\n-   Buffering. Will send accumulated messages on open\n-   Multiple builds available (see dist folder)\n-   Debug mode\n\n## Install\n\n```bash\nnpm install --save @bytemain/reconnecting-websocket\n```\n\n## Usage\n\n### Compatible with WebSocket Browser API\n\nSo this documentation should be valid:\n[MDN WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).\n\nPing me if you find any problems. Or, even better, write a test for your case and make a pull\nrequest :)\n\n### Simple usage\n\n```javascript\nimport ReconnectingWebSocket from '@bytemain/reconnecting-websocket';\n\nconst rws = new ReconnectingWebSocket('ws://my.site.com');\n\nrws.addEventListener('open', () => {\n    rws.send('hello!');\n});\n```\n\n### Update URL\n\nThe `url` parameter will be resolved before connecting, possible types:\n\n-   `string`\n-   `() => string`\n-   `() => Promise<string>`\n\n```javascript\nimport ReconnectingWebSocket from '@bytemain/reconnecting-websocket';\n\nconst urls = ['ws://my.site.com', 'ws://your.site.com', 'ws://their.site.com'];\nlet urlIndex = 0;\n\n// round robin url provider\nconst urlProvider = () => urls[urlIndex++ % urls.length];\n\nconst rws = new ReconnectingWebSocket(urlProvider);\n```\n\n```javascript\nimport ReconnectingWebSocket from '@bytemain/reconnecting-websocket';\n\n// async url provider\nconst urlProvider = async () => {\n    const token = await getSessionToken();\n    return `wss://my.site.com/${token}`;\n};\n\nconst rws = new ReconnectingWebSocket(urlProvider);\n```\n\n### Options\n\n#### Sample with custom options\n\n```javascript\nimport ReconnectingWebSocket from '@bytemain/reconnecting-websocket';\nimport WS from 'ws';\n\nconst options = {\n    WebSocket: WS, // custom WebSocket constructor\n    connectionTimeout: 1000,\n    maxRetries: 10,\n};\nconst rws = new ReconnectingWebSocket('ws://my.site.com', [], options);\n```\n\n#### Available options\n\n```typescript\ntype Options = {\n    WebSocket?: any; // WebSocket constructor, if none provided, defaults to global WebSocket\n    WebSocketOptions?: any; // WebSocket options\n    maxReconnectionDelay?: number; // max delay in ms between reconnections\n    minReconnectionDelay?: number; // min delay in ms between reconnections\n    reconnectionDelayGrowFactor?: number; // how fast the reconnection delay grows\n    minUptime?: number; // min time in ms to consider connection as stable\n    connectionTimeout?: number; // retry connect if not connected after this time, in ms\n    maxRetries?: number; // maximum number of retries\n    maxEnqueuedMessages?: number; // maximum number of messages to buffer until reconnection\n    startClosed?: boolean; // start websocket in CLOSED state, call `.reconnect()` to connect\n    debug?: boolean; // enables debug output\n};\n```\n\n#### Default values\n\n```javascript\nWebSocket: undefined,\nWebSocketOptions: {},\nmaxReconnectionDelay: 10000,\nminReconnectionDelay: 1000 + Math.random() * 4000,\nreconnectionDelayGrowFactor: 1.3,\nminUptime: 5000,\nconnectionTimeout: 4000,\nmaxRetries: Infinity,\nmaxEnqueuedMessages: Infinity,\nstartClosed: false,\ndebug: false,\n```\n\n## API\n\n### Methods\n\n```typescript\nconstructor(url: UrlProvider, protocols?: string | string[], options?: Options)\n\nclose(code?: number, reason?: string)\nreconnect(code?: number, reason?: string)\n\nsend(data: string | ArrayBuffer | Blob | ArrayBufferView)\n\naddEventListener(type: 'open' | 'close' | 'message' | 'error', listener: EventListener)\nremoveEventListener(type:  'open' | 'close' | 'message' | 'error', listener: EventListener)\n```\n\n### Attributes\n\n[More info](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)\n\n```typescript\nbinaryType: string;\nbufferedAmount: number;\nextensions: string;\nonclose: EventListener;\nonerror: EventListener;\nonmessage: EventListener;\nonopen: EventListener;\nprotocol: string;\nreadyState: number;\nurl: string;\nretryCount: number;\n```\n\n### Constants\n\n```text\nCONNECTING 0 The connection is not yet open.\nOPEN       1 The connection is open and ready to communicate.\nCLOSING    2 The connection is in the process of closing.\nCLOSED     3 The connection is closed or couldn't be opened.\n```\n\n## Contributing\n\n[Read here](./CONTRIBUTING.md)\n\n## License\n\nMIT\n\nThis project was forked from [pladaria/reconnecting-websocket](https://github.com/pladaria/reconnecting-websocket).\n\nBecause it seems that the original project is no longer maintained, I decided to fork it and continue to maintain it.\n\nThanks all original contributors for their work.\n","readmeFilename":"README.md"}