10000 GitHub - dimdenGD/recowebsocket: WebSocket with automatic reconnection.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dimdenGD/recowebsocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RecoWebSocket

WebSocket with automatic reconnection. Just a wrapper around ws Client with a bunch of new options.

Installation

npm install recowebsocket

Usage

Everything is the same as in ws Client, but with reconnect, reconnectInterval, reconnectMaxAttempts options. All events you attached will be automatically attached to the underlying ws Client, even on reconnect. Set reconnectMaxAttempts to -1 to reconnect indefinitely.

import WebSocket from "recowebsocket";

const ws = new WebSocket("wss://echo.websocket.org", {
  // these are the default values
  reconnect: true,
  reconnectInterval: 1000,
  reconnectMaxAttempts: -1,
});

ws.on("open", () => {
  console.log("Connected to the server");
  ws.send("Hello, server!");
});

Calling ws.close() or ws.terminate() will close the underlying ws Client and stop reconnecting.

There's also additional nice method ws.json(data, options), which will stringify data and send it as a JSON.

Caveats

  • Not all event emitter methods are supported. Only on, once and off are supported. once will be called once on each reconnect. So it's not actually once I guess.
  • Some obscure ws client stuff might not work.

License

MIT

About

WebSocket with automatic reconnection.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0