Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing a "notification" concept #48

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"max-params": ["error", {"max" : 3}],
"max-statements": ["error", {"max" : 11}, {"ignoreTopLevelFunctions": false}],
"max-len": ["error", {"code" : 120}],
"max-lines": ["error", {"max": 250, "skipComments": true, "skipBlankLines": true}],
"max-lines": ["error", {"max": 1200, "skipComments": true, "skipBlankLines": true}],
"semi": ["error", "always"],
"space-before-function-paren": ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}],
"no-prototype-builtins": 0
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ npm-debug.log
.idea

/.installed/
/dist/websocket-as-promised-2.0.1.js
/promised-websocket-2.0.2.tgz
/yarn.lock
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 2.0.4
* `_handleUnpackedData` method: add parameter check and ignore the handling if the parameter is `undefined`.

## 2.0.1 (Jan 7, 2024)
* `WebSocketAsPromised` adds an `onUnpackedNotif` event handler.
* The original `_tryHandleResponse` method is modified to return a boolean value to reflect whether the given `data` parameter contains an `id` attribute.
* Addition of a browserify script.
* Minor usage instructions added to README.md.

## 1.0.0 (Nov 21, 2019)

* Provide unpacked sources by default to reduce bundle size ([#22])
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Base on `vitalets/websocket-as-promised`.

Introducing a `onUnpackedNotif` event channel. Request/response messages both have an `id` attribute, while notification
messages are messages that do not contain an `id` attribute and can be used for server-initiated notification messages.

> Note: The requestId property on the server side must be named `id`; otherwise, the `onUnpackedNotif` event will be
> fired every time a message is received.

# websocket-as-promised

<img alt="websocket-as-promised logo" align="right" src="https://user-images.githubusercontent.com/1473072/32486445-b2443538-c3b7-11e7-8e9f-94c95efad760.png"/>
Expand Down Expand Up @@ -37,6 +45,7 @@ await wsp.close();
* [JSON](#sending-json)
* [binary](#sending-binary)
* [request / response](#sending-requests)
* [Listen notifications](#listen-notifications)
* [API](#api)
* [Changelog](#changelog)
* [License](#license)
Expand Down Expand Up @@ -180,6 +189,14 @@ wsp.sendRequest({foo: 'bar'}, {requestId: 42});

> Note: you should implement yourself attaching `requestId` on server side.

## Listen notifications
When use send/request pattern, you can listen a notification which the message does not contain a `id` property.
```js
wsp.onUnpackedNotif.addListener(data => console.log(JSON.stringify(data)));
```



## API

### Classes
Expand Down Expand Up @@ -213,6 +230,7 @@ wsp.sendRequest({foo: 'bar'}, {requestId: 42});
* [.onSend](#WebSocketAsPromised+onSend) ⇒ <code>Channel</code>
* [.onMessage](#WebSocketAsPromised+onMessage) ⇒ <code>Channel</code>
* [.onUnpackedMessage](#WebSocketAsPromised+onUnpackedMessage) ⇒ <code>Channel</code>
* [.onUnpackedNotif](#WebSocketAsPromised+onUnpackedNotif) ⇒ <code>Channel</code>
* [.onResponse](#WebSocketAsPromised+onResponse) ⇒ <code>Channel</code>
* [.onClose](#WebSocketAsPromised+onClose) ⇒ <code>Channel</code>
* [.onError](#WebSocketAsPromised+onError) ⇒ <code>Channel</code>
Expand Down Expand Up @@ -317,6 +335,18 @@ For example, if you are using JSON transport, the listener will receive already
```js
wsp.onUnpackedMessage.addListener(data => console.log(data.foo));
```
<a name="WebSocketAsPromised+onUnpackedNotif"></a>

#### wsp.onUnpackedNotif ⇒ <code>Channel</code>
Event channel triggered every time when a notification which does not contain an `id` property is successfully unpacked.
For example, if you are using JSON transport, the listener will receive already JSON parsed data.

**Kind**: instance property of [<code>WebSocketAsPromised</code>](#WebSocketAsPromised)
**See**: https://vitalets.github.io/chnl/#channel
**Example**
```js
wsp.onUnpackedNotif.addListener(data => console.log(data.foo));
```
<a name="WebSocketAsPromised+onResponse"></a>

#### wsp.onResponse ⇒ <code>Channel</code>
Expand Down
1 change: 1 addition & 0 deletions browerify-standalone.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
browserify src/index.js --standalone WebSocketAsPromised -o dist/websocket-as-promised-2.0.1.js
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "websocket-as-promised",
"version": "2.0.1",
"description": "A WebSocket client library providing Promise-based API for connecting, disconnecting and messaging with server",
"name": "promised-websocket",
"version": "2.0.4",
"description": "A WebSocket client library providing Promise-based API for connecting, disconnecting and messaging with server. Original developed by Vitaliy Potapov([email protected]).",
"author": {
"name": "Vitaliy Potapov",
"email": "[email protected]"
"name": "KwanKin Yau",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "git://github.com/vitalets/websocket-as-promised.git"
"url": "https://github.com/kwankin-yau/promised-websocket.git"
},
"engines": {
"node": ">=6"
Expand All @@ -23,11 +23,12 @@
"scripts": {
"lint": "eslint src test",
"test": "mocha test/setup.js test/specs",
"test-installed": "node scripts/install-local && LIB_PATH=../.installed/node_modules/websocket-as-promised npm t",
"setlibpath-win": "SET LIB_PATH=../.installed/node_modules/promised-websocket",
"test-installed": "node scripts/install-local && npm run setlibpath-win && npm t",
"test-ci": "npm run lint && npm test && npm run test-installed",
"docs": "node scripts/docs",
"prerelease": "npm run lint && npm test && npm run test-installed",
"release": "npm version $VER && npm publish",
"release": "npm version --allow-same-version %npm_package_version% && npm publish",
"postrelease": "git push --follow-tags --no-verify",
"release-patch": "VER=patch npm run release",
"release-minor": "VER=minor npm run release"
Expand Down
Loading