Skip to content

ws ​

WebSocket client and server.

Import

ts
import { WebSocket, WebSocketServer, WebSocketClose } from "ws";

Needs the "net" permission in thicket.json.

WebSocket class ​

ts
class WebSocket

A connected WebSocket, returned by WebSocket.connect or a server callback.

WebSocket.connected ​

ts
connected: boolean

True while the connection is established; false after close or error.

WebSocket.remoteAddress ​

ts
readonly remoteAddress: string

Remote peer address (IP or hostname).

WebSocket.remotePort ​

ts
readonly remotePort: int

Remote peer port.

WebSocket.url ​

ts
readonly url: string

The URL passed to connect or received via static method.

WebSocket.close ​

ts
close(code?: int, reason?: string): void

Close 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): void

Register a callback invoked when binary data arrives. @param fn Callback receiving the data as a Buffer.

WebSocket.onClose ​

ts
onClose(fn: any): void

Register a callback invoked when the connection closes. @param fn Callback with no arguments.

WebSocket.onError ​

ts
onError(fn: any): void

Register a callback invoked on connection errors. @param fn Callback receiving an error message string.

WebSocket.onMessage ​

ts
onMessage(fn: any): void

Register a callback invoked when a text message arrives. @param fn Callback receiving the message as a string.

WebSocket.onDrain ​

ts
onDrain(fn: any): void

Register 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): boolean

Send a text message. Returns true if accepted, false if the write buffer is full.

WebSocket.sendBinary ​

ts
sendBinary(data: Buffer): boolean

Send a binary message. Returns true if accepted, false if the write buffer is full.

WebSocket.connect ​

ts
static connect(url: string, options?: WebSocketConnectOptions): any

Connect 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 WebSocketServer

A native listening socket for inbound WebSocket connections. Browser/wasm targets cannot provide this.

WebSocketServer.port ​

ts
readonly port: int

Local port the server is listening on.

WebSocketServer.connections ​

ts
readonly connections: Array<WebSocket>

Array of accepted WebSocket connections.

WebSocketServer.connectionCount ​

ts
readonly connectionCount: int

Current number of connected clients.

WebSocketServer.broadcast ​

ts
broadcast(data: string): boolean

Broadcast a text message to all connected clients. Returns true if all accepts succeeded, false if any failed.

WebSocketServer.close ​

ts
close(): void

Close the server, stopping acceptance of new connections but leaving existing ones open until they close.

WebSocketServer.onConnection ​

ts
onConnection(fn: any): void

Register 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): any

Start 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 WebSocketListenOptions

Type only: the runtime hands you instances; you never construct it yourself.

Validation and resource limits for a listening WebSocket server.

WebSocketListenOptions.host ​

ts
host?: string

Local address to bind to (default "127.0.0.1").

WebSocketListenOptions.tls ​

ts
tls?: WebSocketTlsOptions

TLS certificate and key for a secure wss:// listener.

WebSocketListenOptions.maxConnections ​

ts
maxConnections?: int

Maximum concurrent connections to accept (default unlimited).

WebSocketListenOptions.idleTimeout ​

ts
idleTimeout?: float

Close idle connections after this many seconds of inactivity (default no timeout).

WebSocketListenOptions.maxPendingWriteBytes ​

ts
maxPendingWriteBytes?: int

Maximum 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 WebSocketTlsOptions

Type only: the runtime hands you instances; you never construct it yourself.

Certificate files for a direct TLS WebSocket listener.

WebSocketTlsOptions.cert ​

ts
cert: string

Path to the PEM-encoded certificate file.

WebSocketTlsOptions.key ​

ts
key: string

Path to the PEM-encoded private key file.

WebSocketConnectOptions class ​

ts
class WebSocketConnectOptions

Type only: the runtime hands you instances; you never construct it yourself.

Extra trust anchor for a verified wss:// connection.

WebSocketConnectOptions.ca ​

ts
ca?: string

PEM-encoded CA certificate to trust for the wss:// connection (optional).

WebSocketClose static class ​

ts
static class WebSocketClose

RFC 6455 close codes for WebSocket.close().

WebSocketClose.NORMAL ​

ts
static readonly NORMAL: int

Normal closure; the connection successfully completed its purpose.

WebSocketClose.GOING_AWAY ​

ts
static readonly GOING_AWAY: int

The endpoint is going away, such as by a restart or shutdown.

WebSocketClose.PROTOCOL_ERROR ​

ts
static readonly PROTOCOL_ERROR: int

The endpoint received a malformed frame.

WebSocketClose.UNSUPPORTED_DATA ​

ts
static readonly UNSUPPORTED_DATA: int

The endpoint received an unsupported data type.

WebSocketClose.ABNORMAL_CLOSURE ​

ts
static readonly ABNORMAL_CLOSURE: int

The connection was closed abnormally without a close frame.

WebSocketClose.INVALID_PAYLOAD ​

ts
static readonly INVALID_PAYLOAD: int

The endpoint received a frame with invalid payload data.

WebSocketClose.POLICY_VIOLATION ​

ts
static readonly POLICY_VIOLATION: int

The endpoint received a message violating its policy.

WebSocketClose.MESSAGE_TOO_BIG ​

ts
static readonly MESSAGE_TOO_BIG: int

The endpoint received a message that is too large to process.

WebSocketClose.INTERNAL_ERROR ​

ts
static readonly INTERNAL_ERROR: int

The server encountered an unexpected condition and cannot continue.


Generated from lang/types/ws.d.tks in the ThicketScript repository.