{"id":20529503,"url":"https://github.com/haimkastner/reconnecting-ws","last_synced_at":"2025-04-14T05:16:39.066Z","repository":{"id":44764331,"uuid":"135392673","full_name":"haimkastner/reconnecting-ws","owner":"haimkastner","description":"Wrap WS client to try reconnect websocket server when disconnected by any reason","archived":false,"fork":false,"pushed_at":"2022-01-26T10:21:08.000Z","size":27,"stargazers_count":4,"open_issues_count":2,"forks_count":4,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-14T05:16:28.616Z","etag":null,"topics":["websocket","ws"],"latest_commit_sha":null,"homepage":"","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/haimkastner.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}},"created_at":"2018-05-30T05:33:18.000Z","updated_at":"2023-03-17T08:49:41.000Z","dependencies_parsed_at":"2022-09-26T19:34:40.376Z","dependency_job_id":null,"html_url":"https://github.com/haimkastner/reconnecting-ws","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Freconnecting-ws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Freconnecting-ws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Freconnecting-ws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Freconnecting-ws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/haimkastner","download_url":"https://codeload.github.com/haimkastner/reconnecting-ws/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248824693,"owners_count":21167345,"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":["websocket","ws"],"created_at":"2024-11-15T23:32:27.215Z","updated_at":"2025-04-14T05:16:39.023Z","avatar_url":"https://github.com/haimkastner.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# reconnecting-ws\nWrap WS client to auto reconnect websocket server when disconnected by any reason.\n\nThe code is based on https://github.com/websockets/ws/wiki/Websocket-client-implementation-for-auto-reconnect\n\n\n## Installing\n\n```\nnpm install --save reconnecting-ws\n```\n\n## Using\n\n```typescript\nimport { WebSocketClient } from 'reconnecting-ws';\n\n\nclass SocketClient {\n\n    private webSocketClient: WebSocketClient;\n    constructor() {\n        /** \n         * Init WebSocketClient\n         * Set trying reconnect when connection lost each 5 seconds, and show console message \n         */\n        this.webSocketClient = new WebSocketClient(5000, true);\n\n        /** Subscribe to emitters. */\n        this.webSocketClient.on('open', this.onOpen);\n        this.webSocketClient.on('error', this.onError);\n        this.webSocketClient.on('message', this.onMessage);\n        this.webSocketClient.on('close', this.onClose);\n        this.webSocketClient.on('reconnect', this.onReconnect);\n\n        /** Connect to server. */\n        this.webSocketClient.connect('ws://127.0.0.1:3001');\n\n        /** \n         * It`s possible to access WebSocket instance directly.\n         * without using wrapper of WebSocketClient. (see https://github.com/websockets/ws).\n         * Note that when trying to reconnect *all* listeners will be removed\n         */\n        const bufferAmount = this.webSocketClient.WebSocketInstance.bufferedAmount;\n\n        /** Disconnect manually in minute. */\n        setTimeout(() =\u003e {\n            this.webSocketClient.disconnect();\n        }, 60000);\n    }\n\n    private onOpen() {\n        // TODO\n        console.log('onOpen');\n    }\n\n    private onClose(code: number, reason: string) {\n        // TODO\n        console.log('onClose');\n    }\n\n    private onError(err: Error) {\n        // TODO\n        console.log('onError');\n    }\n\n    private onMessage(data: WebSocket.Data) {\n        // TODO\n        console.log('onMessage');\n\n    }\n\n    private onReconnect() {\n        // TODO\n        console.log('onReconnect');\n\n    }\n}\n```\n\nFor real example see `example` folder.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaimkastner%2Freconnecting-ws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhaimkastner%2Freconnecting-ws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaimkastner%2Freconnecting-ws/lists"}