Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
websocket-ts
Advanced tools
A client-websocket written in TypeScript to be used from within browsers with focus on simplicity, reliability and extensibility. It provides convenient features to automatically reconnect and buffer pending messages.
A client-websocket written in TypeScript to be used from within browsers with focus on simplicity, reliability and extensibility. It provides convenient features to automatically reconnect and buffer pending messages.
New instances can be easily created through the Builder-class.
const ws = new Builder('ws://localhost:42421').build();
You can register callbacks for onOpen
-, onClose
-, onError
- and onMessage
-events. The callbacks get called with the websocket-instance that caused the event plus the event as parameters.
const ws = new Builder('ws://localhost:42421')
.onOpen((i, e) => { console.log("opened") })
.onClose((i, e) => { console.log("closed") })
.onError((i, e) => { console.log("error") })
.onMessage((i, e) => { i.send(e.data) })
.build();
It is possible to register multiple callbacks for the same event, they are called in stack-order:
const ws = new Builder('ws://localhost:42421')
.onMessage((i, e) => { console.log("sent echo") })
.onMessage((i, e) => { i.send(e.data) })
.onMessage((i, e) => { console.log("message received") })
.build();
To buffer pending messages while your websocket is disconnected, configure it to use a Buffer. These pending messages will be sent out as soon as the connection is (re)-established.
const ws = new Builder('ws://localhost:42421')
.withBuffer(new LRUBuffer(100)) // buffers up to 100 messages, substitutes old messages with new ones
.build();
const ws = new Builder('ws://localhost:42421')
.withBuffer(new TimeBuffer(5 * 60 * 1000)) // buffers messages that were written within the last 5 minutes
.build();
To configure the websocket to automatically reconnect when the connection gets lost, provide it with a Backoff. The type of backoff provided decides the delay between connection-retries.
const ws = new Builder('ws://localhost:42421')
.withBackoff(new ConstantBackoff(500)) // Always waits 500 ms between retries
.build();
const ws = new Builder('ws://localhost:42421')
.withBackoff(new ExponentialBackoff(100)) // Doubles the time between reconnects with every try
.build();
FAQs
<img src="https://github.com/jjxxs/websocket-ts
The npm package websocket-ts receives a total of 3,067 weekly downloads. As such, websocket-ts popularity was classified as popular.
We found that websocket-ts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.