ltws is a lightweight WebSocket library that provides automatic reconnection and promise-based async connection. It wraps the native WebSocket API and adds event-driven communication, automatic ping/pong keep-alive, and customizable retry logic. Version 1.0.9 is stable. It offers a simple API with connect, send, sendJson, and event listeners for connect, disconnect, message, json, and errors. Key differentiators: built-in reconnection with configurable delays and retries, support for both callback and async/await patterns, and TypeScript-like type definitions.
npm install ltwsNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Connects to an echo WebSocket server, sends a text message and a JSON message, logs incoming messages, and disconnects after 5 seconds.
Replace new ltws() with ltws.connect(url) or ltws.createInstance().
Set useReconnectEvent to false (default) and listen for 'reconnect' event directly.
Call JSON.parse(JSON.stringify(obj)) before passing to sendJson if you need to preserve immutability.
Run npm install ws alongside ltws.
npm install ws
Use ltws.connect(url) or ltws.createInstance() instead.
Wrap send calls inside the 'connect' event listener, or use connectAsync and await.
Increase connectTimeout option or check server availability.