Skip to content

Commit

Permalink
add websocket server documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tedeh committed Jun 26, 2021
1 parent 704dd83 commit 32bd032
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ client.request('multiply', [5, 5], function(err, error, result) {

*Since v3.6.4*

Experimental websocket client that wraps around an `isomorphic-ws` instance. Will listen to every received (JSON) message and see if it matches any of the currently outstanding requests made, in which case the callback of that outstanding request will fire. If you do not provide the `timeout` option it will wait forever. Has a promise-based equivalent receiving the same options, and a companion jayson server where you can find an example and more information.
Experimental websocket client that wraps around an `isomorphic-ws` instance. Will listen to every received (JSON) message and see if it matches any of the currently outstanding requests made, in which case the callback of that outstanding request will fire. If you do not provide the `timeout` option it will wait forever. Has a promise-based equivalent receiving the same options, and a [companion jayson server](#serverwebsocket) where you can find an example.

Has the following options:

Expand All @@ -338,6 +338,8 @@ Has the following options:

If you want to "unwrap" the `isomorphic-ws` instance you can use the `Client.websocket.prototype.unlisten` which stops listening for messages on the `isomorphic-ws` instance.

- [isomorphic-ws docs](https://github.com/heineiuo/isomorphic-ws)

#### Notifications

Notification requests are for cases where the reply from the server is not important and should be ignored. This is accomplished by setting the `id` property of a request object to `null`.
Expand Down Expand Up @@ -455,8 +457,10 @@ The server also sports several interfaces that can be accessed as properties of
| `Server.tls` | TLS server that inherits from [tls.Server][nodejs_doc_tls_server] |
| `Server.http` | HTTP server that inherits from [http.Server][nodejs_doc_http_server] |
| `Server.https` | HTTPS server that inherits from [https.Server][nodejs_doc_https_server] |
| `Server.websocket` | Websocket server that uses [isomorphic-ws Server][isomorphic-ws-docs] |
| `Server.middleware` | Method that returns a [Connect][connect]/[Express][express] compatible middleware function |

[isomorphic-ws-docs]: https://github.com/heineiuo/isomorphic-ws
[nodejs_doc_net_server]: http://nodejs.org/docs/latest/api/net.html#net_class_net_server
[nodejs_doc_http_server]: http://nodejs.org/docs/latest/api/http.html#http_class_http_server
[nodejs_doc_https_server]: http://nodejs.org/docs/latest/api/https.html#https_class_https_server
Expand Down Expand Up @@ -532,6 +536,51 @@ app.use(server.middleware());
app.listen(3000);
```

##### Server.websocket

Websocket server that either wraps around a provided `require('isomorphic-ws').Server` instance or creates one from scratch. Expects **every** incoming message on every connection to be a valid JSON-RPC call.

The websocket server supports the following options in addition to the base class:

| Option | Default | Type | Description |
|--------|-------------|-----------------------------------|---------------------------------|
| `wss` | `undefined` | `require('isomorphic-ws').Server` | If not provided will be created |


Websocket server example in [examples/websocket/server.js](examples/websocket/server.js):

```javascript
const jayson = require('jayson');

const server = new jayson.Server({
add: function (args, done) {
const sum = args.reduce((sum, val) => sum + val, 0);
done(null, sum);
},
});

const wss = server.websocket({
port: 12345,
});
```

Websocket client example in [examples/websocket/client.js](examples/websocket/client.js):

```javascript
const jayson = require('jayson');

const client = jayson.client.websocket({
url: 'ws://localhost:12345',
});

client.ws.on('open', function () {
client.request('add', [1,2,3,4], function (err, result) {
console.log(err, result);
client.ws.close();
});
});
```

#### Many interfaces at the same time

A Jayson server can use many interfaces at the same time.
Expand Down
1 change: 0 additions & 1 deletion examples/websocket/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const WebSocket = require('isomorphic-ws');
const jayson = require('../../');

const client = jayson.client.websocket({
Expand Down
1 change: 0 additions & 1 deletion examples/websocket/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const WebSocket = require('ws');
const jayson = require('../../');

const server = new jayson.Server({
Expand Down

0 comments on commit 32bd032

Please sign in to comment.