liwi-resources-websocket-client-node
node websocket client implementation for liwi
About
Node TransportClient, to be used against liwi-resources-websocket-server. It is liwi-resources-websocket-client wired for a node host:
- connects through
wsrather than node's globalWebSocket, which cannot set handshake headers, - so authentication headers can be sent on the upgrade request,
- and page-visibility awareness is disabled, so reconnection never waits for a
visibilitychangethat will not come.
Install
npm install --save liwi-resources-websocket-client-node
Usage
import { createNodeWebsocketTransportClient } from "liwi-resources-websocket-client-node";
const transportClient = createNodeWebsocketTransportClient({
url: "ws://localhost:4005/ws",
headers: () => ({ authorization: `Bearer ${getToken()}` }),
onConnectionFailure: (failure) => {
if (failure.type === "handshake" && failure.status === 401) promptSignIn();
},
});
transportClient.connect();
const tasksService = createTasksServiceClient(transportClient);
Options
Everything WebsocketTransportClientOptions accepts, except thirdWebsocketArgument, getThirdWebsocketArgument and visibilitySource, which this package sets itself. Plus:
| Option | Default | Description |
|---|---|---|
url | — (required) | Websocket endpoint. There is no location to derive it from |
headers | — | Handshake headers, or a function returning them |
webSocketOptions | — | Extra ws client options |
webSocketImplementation | ws | WebSocket constructor to use |
Pass headers as a function when the token can expire: it is evaluated on every connection attempt, so reconnections carry a refreshed token rather than the one captured at creation.
webSocketOptions.headers are merged underneath headers, so headers wins on a key collision.
Expired tokens
A rejected upgrade is surfaced through onConnectionFailure with the HTTP status, and a 4xx stops the reconnection loop instead of retrying forever. See connection failures.