Skip to content

Commit 5f2ba5c

Browse files
authored
docs(client, client-web-socket-node): extend web socket support docs (#256)
1 parent 3764a6c commit 5f2ba5c

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

packages/client-web-socket-node/mod.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
/**
2-
* WebSocket adapter for Node.js environment. Build on top of `npm:ws`.
2+
* Client WebSocket adapter for Node.js (and Bun). Wrapper over the [`npm:ws`](https://www.npmjs.com/package/ws).
3+
*
4+
* @example Use with `Client`
5+
*
6+
* ```ts
7+
* import ws from '@iroha/client-web-socket-node'
8+
* import { Client, type CreateClientParams } from '@iroha/client'
9+
*
10+
* function createClient(params: CreateClientParams) {
11+
* const client = new Client({
12+
* ...params,
13+
*
14+
* // provide WebSocket adapter here
15+
* ws,
16+
* })
17+
* }
18+
* ```
19+
*
20+
* @example Use with `WebSocketAPI`
321
*
4-
* @example
522
* ```ts
623
* import ws from '@iroha/client-web-socket-node'
724
* import { WebSocketAPI } from '@iroha/client'

packages/client/mod.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,41 @@
33
*
44
* The primary functionality is exposed via the {@linkcode Client}.
55
*
6+
* ## WebSocket Support
7+
*
8+
* Some APIs (e.g. {@linkcode Client#events}, {@linkcode Client#blocks}) require {@link WebSocket} support.
9+
*
10+
* WebSocket is not supported uniformly across environments, so adapters are used to provide a consistent interface.
11+
*
12+
* In Deno and browsers, no setup is needed - the adapter over the native {@link WebSocket} is used by default.
13+
* In Node and Bun, you **must** explicitly provide a compatible WebSocket adapter.
14+
*
15+
* You can also implement your own by conforming to the {@linkcode [web-socket].IsomorphicWebSocketAdapter} interface.
16+
*
17+
* In summary:
18+
*
19+
* | Environment | Adapter |
20+
* | - | - |
21+
* | Deno, Web (browsers) | Native adapter used by default ({@linkcode [web-socket].nativeWS}) |
22+
* | Node, Bun | Use [`@iroha/client-web-socket-node`](https://jsr.io/@iroha/client-web-socket-node) |
23+
* | Custom | Implement {@linkcode [web-socket].IsomorphicWebSocketAdapter} |
24+
*
25+
* ### Example: using `@iroha/client-web-socket-node`
26+
*
27+
* ```ts
28+
* import ws from '@iroha/client-web-socket-node'
29+
* import { Client, type CreateClientParams } from '@iroha/client'
30+
*
31+
* function createClient(params: CreateClientParams) {
32+
* const client = new Client({
33+
* ...params,
34+
*
35+
* // provide WebSocket adapter here
36+
* ws,
37+
* })
38+
* }
39+
* ```
40+
*
641
* @example Construct a client with a random account
742
* ```ts
843
* import { Client } from '@iroha/client'

packages/client/web-socket/native.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ async function handleIncomingData(data: any): Promise<IncomingData> {
1010
throw new Error('Unable to parse incoming data')
1111
}
1212

13+
/**
14+
* Implementation of {@linkcode IsomorphicWebSocketAdapter} based on the native {@link WebSocket}.
15+
*/
1316
const adapter: IsomorphicWebSocketAdapter = {
1417
initWebSocket: (params) => {
1518
const socket = new WebSocket(params.url)

0 commit comments

Comments
 (0)