Skip to content

Commit

Permalink
v1.1.1 (#14)
Browse files Browse the repository at this point in the history
* Bumped devDependencies

* Changed config due to breaking change in jest v27.0.0

* Added documentation

* Refactoring, simplified names
  • Loading branch information
jjxxs authored Jun 3, 2021
1 parent c192250 commit 0b9673c
Show file tree
Hide file tree
Showing 5 changed files with 1,026 additions and 1,583 deletions.
67 changes: 38 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# websocket-ts
A client-websocket written in TypeScript meant 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 for browser-applications. Focus is on simplicity, reliability and extensibility. It provides convenient features to automatically reconnect and buffer pending messages.

[![Build Status](https://travis-ci.org/jjxxs/websocket-ts.svg?branch=master)](https://travis-ci.org/jjxxs/websocket-ts)
[![Coverage Status](https://coveralls.io/repos/github/jjxxs/websocket-ts/badge.svg?branch=master&service=github)](https://coveralls.io/github/jjxxs/websocket-ts?branch=master)
Expand Down Expand Up @@ -32,7 +32,7 @@ import {WebsocketBuilder} from 'websocket-ts';
## Usage

#### Initialization
New instances are most easily created with the provided `WebsocketBuilder`:
Create a new instance with the provided `WebsocketBuilder`:

```typescript
const ws = new WebsocketBuilder('ws://localhost:42421').build();
Expand Down Expand Up @@ -69,48 +69,32 @@ const ws = new WebsocketBuilder('ws://localhost:42421')
.build();
```

#### Send & buffer messages
To send messages, use the websockets `send()`-method:
You can remove event-listener with `removeEventListener`:
```typescript
let ws: Websocket;
let ws: Websocket
/* ... */
ws.send("Hello World!");
```

If you want to buffer to-be-send messages while the websocket is disconnected, you can provide it with a `Buffer`.
The websocket will then use the buffer to temporarily keep your messages and send them in correct order once the
connection is re-established. There are currently two `Buffer`-implementations. You can also implement your own
by inheriting from the `Buffer`-interface.

##### LRUBuffer
The `LRUBuffer` keeps the last `n` messages. When the buffer is full, the oldest message in the buffer will be replaced.
It uses an array as a circular-buffer for linear space- and time-requirements. To use the `LRUBuffer` with a capacity of `1000`:
```typescript
const ws = new WebsocketBuilder('ws://localhost:42421')
.withBuffer(new LRUBuffer(1000))
.build();
ws.removeEventListener(WebsocketEvents.open, openEventListener);
```

##### TimeBuffer
The `TimeBuffer` will keep all messages that were written within the last `n` milliseconds. It will drop messages that are
older than the specified amount. To use the `TimeBuffer` that keeps messages from the last `5 minutes`:
#### Send
To send messages, use the websockets `send()`-method:
```typescript
const ws = new WebsocketBuilder('ws://localhost:42421')
.withBuffer(new TimeBuffer(5 * 60 * 1000))
.build();
let ws: Websocket;
/* ... */
ws.send("Hello World!");
```

#### Reconnect & Backoff
If you want the websocket to automatically try to re-connect when the connection is lost, you can provide it with a `Backoff`.
The websocket will use the `Backoff` to determine how long it should wait between re-tries. There are currently three
The websocket will use the `Backoff` to determine how long it should wait between re-tries. There are currently three
`Backoff`-implementations. You can also implement your own by inheriting from the `Backoff`-interface.

##### ConstantBackoff
The `ConstantBackoff` will make the websocket wait a constant time between each connection retry. To use the `ConstantBackoff`
with a wait-time of `1 second`:
```typescript
const ws = new WebsocketBuilder('ws://localhost:42421')
.withBackoff(new ConstantBackoff(500))
.withBackoff(new ConstantBackoff(1000)) // 1000ms = 1s
.build();
```

Expand All @@ -134,5 +118,30 @@ const ws = new WebsocketBuilder('ws://localhost:42421')
.build();
```

#### Buffer

If you want to buffer to-be-send messages while the websocket is disconnected, you can provide it with a `Buffer`.
The websocket will use the buffer to temporarily keep your messages and send them in order once the websocket
(re-)connects. There are currently two `Buffer`-implementations. You can also implement your own
by inheriting from the `Buffer`-interface.

##### LRUBuffer
The `LRUBuffer` keeps the last `n` messages. When the buffer is full, the oldest message in the buffer will be replaced.
It uses an array as a circular-buffer for linear space- and time-requirements. To use the `LRUBuffer` with a capacity of `1000`:
```typescript
const ws = new WebsocketBuilder('ws://localhost:42421')
.withBuffer(new LRUBuffer(1000))
.build();
```

##### TimeBuffer
The `TimeBuffer` will keep all messages that were written within the last `n` milliseconds. It will drop messages that are
older than the specified amount. To use the `TimeBuffer` that keeps messages from the last `5 minutes`:
```typescript
const ws = new WebsocketBuilder('ws://localhost:42421')
.withBuffer(new TimeBuffer(5 * 60 * 1000))
.build();
```

#### Build & Tests
To build the project run `yarn build`. All provided components are covered with unit-tests. To run the tests run `yarn test`.
To build the project run `yarn build`. All provided components are covered with unit-tests. Run the tests with `yarn test`.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = {
},
testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
collectCoverage: true
collectCoverage: true,
testEnvironment: "jsdom"
};
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "websocket-ts",
"version": "1.1.0",
"version": "1.1.1",
"main": "lib/index.js",
"types": "lib/",
"license": "MIT",
Expand All @@ -22,12 +22,12 @@
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls"
},
"devDependencies": {
"@types/jest": "^26.0.13",
"@types/ws": "^7.2.6",
"@types/jest": "^26.0.23",
"@types/ws": "^7.4.4",
"coveralls": "^3.1.0",
"jest": "^26.4.2",
"ts-jest": "^26.3.0",
"typescript": "^4.0.2",
"ws": "^7.3.1"
"jest": "^27.0.4",
"ts-jest": "^27.0.2",
"typescript": "^4.3.2",
"ws": "^7.4.6"
}
}
10 changes: 5 additions & 5 deletions test/websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,20 @@ describe("Testsuite for Websocket", () => {

test("Websocket should remove event-listener correctly when removeEventListener() is called", async () => {
let count = 0;
const onOpenEventListener = () => count++; // increment counter on every connect
const openEventListener = () => count++; // increment counter on every connect
let onOpen: () => void;
let onClose: () => void;
let wsOnOpenPromise = new Promise<void>(resolve => onOpen = resolve);
const wsOnClosePromise = new Promise<void>(resolve => onClose = resolve);
ws = new WebsocketBuilder(url)
.withBackoff(new ConstantBackoff(100))
.onOpen(() => onOpen())
.onOpen(onOpenEventListener)
.onOpen(openEventListener)
.onClose(() => onClose())
.build();
await wsOnOpenPromise; // wait for initial connection
expect(count).toBe(1); // openEventListener should be called exactly once at this point
ws.removeEventListener(WebsocketEvents.open, onOpenEventListener); // unregister the event-handler
ws.removeEventListener(WebsocketEvents.open, openEventListener); // unregister the event-handler
if (wss !== undefined) // shutdown the server
await shutdownServerOrTimeout(wss, 100);
await wsOnClosePromise; // wait for client to register the disconnect
Expand All @@ -236,15 +236,15 @@ describe("Testsuite for Websocket", () => {

test("Websocket should remove event-listeners if they declare the 'once'-property as true", async () => {
let count = 0;
const onOpenEventListener = () => count++; // increment counter on every connect
const openEventListener = () => count++; // increment counter on every connect
let onOpen: () => void;
let onClose: () => void;
let wsOnOpenPromise = new Promise<void>(resolve => onOpen = resolve);
const wsOnClosePromise = new Promise<void>(resolve => onClose = resolve);
ws = new WebsocketBuilder(url)
.withBackoff(new ConstantBackoff(100))
.onOpen(() => onOpen())
.onOpen(onOpenEventListener, {once: true} as AddEventListenerOptions) // declare 'once'-property
.onOpen(openEventListener, {once: true} as AddEventListenerOptions) // declare 'once'-property
.onClose(() => onClose())
.build();
await wsOnOpenPromise; // wait for initial connection
Expand Down
Loading

0 comments on commit 0b9673c

Please sign in to comment.