ws
WebSocket client and server.
Import
ts
import { WebSocket, WebSocketServer, WebSocketClose } from "ws";Needs the "net" permission in thicket.json.
WebSocket class
ts
class WebSocketA connected WebSocket, returned by WebSocket.connect or a server callback.
WebSocket.connected
ts
connected: booleanTrue while the connection is established; false after close or error.
WebSocket.remoteAddress
ts
readonly remoteAddress: stringRemote peer address (IP or hostname).
WebSocket.remotePort
ts
readonly remotePort: intRemote peer port.
WebSocket.url
ts
readonly url: stringThe URL passed to connect or received via static method.
WebSocket.close
ts
close(code?: int, reason?: string): voidClose the connection with an optional RFC 6455 close code and reason. @param code Close code (default WebSocketClose.NORMAL). @param reason Close reason text (default empty).
WebSocket.onBinary
ts
onBinary(fn: any): voidRegister a callback invoked when binary data arrives. @param fn Callback receiving the data as a Buffer.
WebSocket.onClose
ts
onClose(fn: any): voidRegister a callback invoked when the connection closes. @param fn Callback with no arguments.
WebSocket.onError
ts
onError(fn: any): voidRegister a callback invoked on connection errors. @param fn Callback receiving an error message string.
WebSocket.onMessage
ts
onMessage(fn: any): voidRegister a callback invoked when a text message arrives. @param fn Callback receiving the message as a string.
WebSocket.onDrain
ts
onDrain(fn: any): voidRegister a callback invoked when write backpressure clears and the socket is writable again. @param fn Callback with no arguments.
WebSocket.send
ts
send(data: string): booleanSend a text message. Returns true if accepted, false if the write buffer is full.
WebSocket.sendBinary
ts
sendBinary(data: Buffer): booleanSend a binary message. Returns true if accepted, false if the write buffer is full.
WebSocket.connect
ts
static connect(url: string, options?: WebSocketConnectOptions): anyConnect to a WebSocket server at the given URL. Returns a promise that resolves to a WebSocket or rejects on connection failure.
Example
ts
import { WebSocket } from "ws";
const socket: WebSocket = await WebSocket.connect("ws://localhost:8080");
socket.onMessage((message: string): void => { console.log(message); });
socket.send("hello");WebSocketServer class
ts
class WebSocketServerA native listening socket for inbound WebSocket connections. Browser/wasm targets cannot provide this.
WebSocketServer.port
ts
readonly port: intLocal port the server is listening on.
WebSocketServer.connections
ts
readonly connections: Array<WebSocket>Array of accepted WebSocket connections.
WebSocketServer.connectionCount
ts
readonly connectionCount: intCurrent number of connected clients.
WebSocketServer.broadcast
ts
broadcast(data: string): booleanBroadcast a text message to all connected clients. Returns true if all accepts succeeded, false if any failed.
WebSocketServer.close
ts
close(): voidClose the server, stopping acceptance of new connections but leaving existing ones open until they close.
WebSocketServer.onConnection
ts
onConnection(fn: any): voidRegister a callback invoked each time a client connects. @param fn Callback receiving the connected WebSocket.
WebSocketServer.listen
ts
static listen(port: int, host?: string | WebSocketListenOptions, options?: WebSocketListenOptions): anyStart a WebSocket listener on the given port. Returns a promise that resolves to a WebSocketServer or rejects if the port cannot be bound.
Example
ts
import { WebSocketServer, WebSocket } from "ws";
const server: WebSocketServer = await WebSocketServer.listen(8080);
server.onConnection((peer: WebSocket): void => {
peer.onMessage((message: string): void => { peer.send(message); });
});WebSocketListenOptions class
ts
class WebSocketListenOptionsType only: the runtime hands you instances; you never construct it yourself.
Validation and resource limits for a listening WebSocket server.
WebSocketListenOptions.host
ts
host?: stringLocal address to bind to (default "127.0.0.1").
WebSocketListenOptions.tls
ts
tls?: WebSocketTlsOptionsTLS certificate and key for a secure wss:// listener.
WebSocketListenOptions.maxConnections
ts
maxConnections?: intMaximum concurrent connections to accept (default unlimited).
WebSocketListenOptions.idleTimeout
ts
idleTimeout?: floatClose idle connections after this many seconds of inactivity (default no timeout).
WebSocketListenOptions.maxPendingWriteBytes
ts
maxPendingWriteBytes?: intMaximum bytes permitted in the send buffer per connection before backpressure is applied (default no limit).
WebSocketListenOptions.supportedProtocols
ts
supportedProtocols?: Array<string> | ((origin: string) => boolean)List of supported WebSocket subprotocols (RFC 6455), or a function to validate the Origin header.
WebSocketListenOptions.verifyOrigin
ts
verifyOrigin?: Array<string> | ((origin: string) => boolean)List of allowed origins for connections, or a function to validate dynamically.
WebSocketTlsOptions class
ts
class WebSocketTlsOptionsType only: the runtime hands you instances; you never construct it yourself.
Certificate files for a direct TLS WebSocket listener.
WebSocketTlsOptions.cert
ts
cert: stringPath to the PEM-encoded certificate file.
WebSocketTlsOptions.key
ts
key: stringPath to the PEM-encoded private key file.
WebSocketConnectOptions class
ts
class WebSocketConnectOptionsType only: the runtime hands you instances; you never construct it yourself.
Extra trust anchor for a verified wss:// connection.
WebSocketConnectOptions.ca
ts
ca?: stringPEM-encoded CA certificate to trust for the wss:// connection (optional).
WebSocketClose static class
ts
static class WebSocketCloseRFC 6455 close codes for WebSocket.close().
WebSocketClose.NORMAL
ts
static readonly NORMAL: intNormal closure; the connection successfully completed its purpose.
WebSocketClose.GOING_AWAY
ts
static readonly GOING_AWAY: intThe endpoint is going away, such as by a restart or shutdown.
WebSocketClose.PROTOCOL_ERROR
ts
static readonly PROTOCOL_ERROR: intThe endpoint received a malformed frame.
WebSocketClose.UNSUPPORTED_DATA
ts
static readonly UNSUPPORTED_DATA: intThe endpoint received an unsupported data type.
WebSocketClose.ABNORMAL_CLOSURE
ts
static readonly ABNORMAL_CLOSURE: intThe connection was closed abnormally without a close frame.
WebSocketClose.INVALID_PAYLOAD
ts
static readonly INVALID_PAYLOAD: intThe endpoint received a frame with invalid payload data.
WebSocketClose.POLICY_VIOLATION
ts
static readonly POLICY_VIOLATION: intThe endpoint received a message violating its policy.
WebSocketClose.MESSAGE_TOO_BIG
ts
static readonly MESSAGE_TOO_BIG: intThe endpoint received a message that is too large to process.
WebSocketClose.INTERNAL_ERROR
ts
static readonly INTERNAL_ERROR: intThe server encountered an unexpected condition and cannot continue.
Generated from lang/types/ws.d.tks in the ThicketScript repository.