1.0.0 • Published 9 months ago

@sergtyapkin/reconnecting-websocket v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

Reconnecting WebSocket

Static Badge npm

WebSocket that will try to reconnects to server when lost connection.

First reconnection attempt after reconnectTimeout milliseconds. With each subsequent reconnection, the timeout will increase by x2 but max reconnection timeout is maxReconnectTimeout.

Default values:

reconnectTimeout = 1000
maxReconnectTimeout = 4000

Example without Vue framework:

import WS from "@sergtyapkin/reconnecting-websocket";

WS.handlers.some_event = (jsonData) => {console.log(jsonData)};
// WS.onopen = (event) => {};
// WS.onerror = (event) => {};
// WS.onclose = (event) => {};

// to disable some handler
delete WS.handlers.some_event;

Example entrypoint using Vue.js:

// index.js

import { createApp } from 'vue';
import App from './App.vue';
import WS from "@sergtyapkin/reconnecting-websocket";

const app = createApp(App)
  .use(WS, `wss://${location.host}/ws`)
  .mount('#app');

// Now WS class available in any child component by:
// this.$ws
1.0.0

9 months ago