ws
The ws module provides WebSocket client and server APIs for JSH applications.
It exposes WebSocket and WebSocketServer classes built on top of the native Go WebSocket implementation.
Typical usage looks like this.
const { WebSocket, WebSocketServer } = require('ws');WebSocket
Creates a WebSocket client connection.
Syntax
new WebSocket(url)
new WebSocket(url, protocol)
new WebSocket(url, protocols)Parameters
urlString: WebSocket server address such asws://host:port/pathorwss://host:port/path.protocolString: one requested subprotocol.protocolsString[]: list of requested subprotocols.
If url is missing or is not a string, the constructor throws TypeError.
Properties
| Property | Type | Description |
|---|---|---|
url | String | Original WebSocket URL passed to the constructor. |
protocol | String | Negotiated subprotocol, or empty string. |
readyState | Number | Current connection state. |
readyState values
WebSocket.CONNECTING=0WebSocket.OPEN=1WebSocket.CLOSING=2WebSocket.CLOSED=3
Message type constants
WebSocket.TextMessage=1WebSocket.BinaryMessage=2
send()
Sends a message to the server.
Syntax
ws.send(data)Parameters
dataString: Message text to send.
In the current JSH implementation, send() is primarily intended for text messages.
If the socket is not open, the module emits an error event.
Usage example
| |
close()
Closes the current connection.
Syntax
ws.close()Calling close() transitions the socket through CLOSING to CLOSED and emits close.
Events
WebSocket extends EventEmitter and supports normal event-listener patterns such as on() and addListener().
open
Emitted after the client successfully connects.
ws.on('open', () => {
console.println('connected');
});close
Emitted when the connection closes.
ws.on('close', () => {
console.println('closed');
});message
Emitted when a message arrives from the server.
The callback receives an event-like object with these fields.
| Field | Type | Description |
|---|---|---|
type | Number | Message type such as WebSocket.TextMessage or WebSocket.BinaryMessage. |
data | String or byte data | Message payload. Text messages are exposed as strings. |
| |
error
Emitted when connection or send operations fail.
ws.on('error', (err) => {
console.println(err.message);
});WebSocketServer
Creates a WebSocket server attached to an existing http.Server.
Syntax
new WebSocketServer(options)Main options
server: attachedhttp.Serverinstance. Required.path: accepted WebSocket path. Default is/.clientTracking: keeps theclientsset whentrue. Default istrue.verifyClient({ origin, req }): synchronously decides whether to accept the handshake. Returningfalserejects the request.handleProtocols(protocols, req): selects one value from requested subprotocols.
Properties
| Property | Type | Description |
|---|---|---|
server | http.Server | Attached HTTP server. |
path | String | Accepted WebSocket path. |
clients | Set | Connected client set. null when clientTracking === false. |
WebSocketServer events
connection(socket, request)error(err)close()
connection request object
The request value passed to connection is a helper object similar to Node.js IncomingMessage.
Main properties:
urlmethodheadersrawHeaderspathhostrequestUrihttpVersioncompleteremoteAddresssocket.remoteAddress
Main methods:
query(name)getHeader(name)hasHeader(name)
Usage example
| |
Usage example
| |
Behavior notes
- Client connection establishment is started asynchronously from the constructor.
- Connection failures are reported through the
errorevent. - Incoming messages may be text or binary, but the JavaScript
send()method is intended for text message usage. WebSocketServerprovides a higher-level API attached tohttp.Server, rather than exposing low-levelupgradehandling.verifyClient()andhandleProtocols()are synchronous.- Promise-based or
await-style asynchronous flows are not supported insideverifyClient()andhandleProtocols(). requestis a JSH helper object, not a full Node.jsIncomingMessageimplementation. It exposes the main fields and methods such asurl,headers,query(), andgetHeader().clientTrackingmaintains theclientsset, but it does not add low-level socket management APIs.- Low-level APIs such as
upgrade,handleUpgrade(), andnoServerare not currently provided.