# Interface WebSocketBehavior<UserData>

A structure holding settings and handlers for a WebSocket URL route handler.

interface WebSocketBehavior<UserData> {
close?: (
ws: WebSocket<UserData>,
code: number,
message: ArrayBuffer,
) => void;
closeOnBackpressureLimit?: boolean;
compression?: number;
drain?: (ws: WebSocket<UserData>) => void;
dropped?: (
ws: WebSocket<UserData>,
message: ArrayBuffer,
isBinary: boolean,
) => void | Promise<void>;
idleTimeout?: number;
maxBackpressure?: number;
maxLifetime?: number;
maxPayloadLength?: number;
message?: (
ws: WebSocket<UserData>,
message: ArrayBuffer,
isBinary: boolean,
) => void | Promise<void>;
open?: (ws: WebSocket<UserData>) => void | Promise<void>;
ping?: (ws: WebSocket<UserData>, message: ArrayBuffer) => void;
pong?: (ws: WebSocket<UserData>, message: ArrayBuffer) => void;
sendPingsAutomatically?: boolean;
subscription?: (
ws: WebSocket<UserData>,
topic: ArrayBuffer,
newCount: number,
oldCount: number,
) => void;
upgrade?: (
res: HttpResponse,
req: HttpRequest,
context: us_socket_context_t,
) => void | Promise<void>;
}

#### Type Parameters

- UserData

- Defined in index.d.ts:274

##### Index

### Properties

close?
closeOnBackpressureLimit?
compression?
drain?
dropped?
idleTimeout?
maxBackpressure?
maxLifetime?
maxPayloadLength?
message?
open?
ping?
pong?
sendPingsAutomatically?
subscription?
upgrade?

## Properties

### `Optional` close

close?: (ws: WebSocket<UserData>, code: number, message: ArrayBuffer) => void

Handler for close event, no matter if error, timeout or graceful close. You may not use WebSocket after this event. Do not send on this WebSocket from within here, it is closed.

- Defined in index.d.ts:304

### `Optional` closeOnBackpressureLimit

closeOnBackpressureLimit?: boolean

Whether or not we should automatically close the socket when a message is dropped due to backpressure. Defaults to false.

- Defined in index.d.ts:278

### `Optional` compression

compression?: number

What permessage-deflate compression to use. uWS.DISABLED, uWS.SHARED_COMPRESSOR or any of the uWS.DEDICATED_COMPRESSOR_xxxKB. Defaults to uWS.DISABLED.

- Defined in index.d.ts:286

### `Optional` drain

drain?: (ws: WebSocket<UserData>) => void

Handler for when WebSocket backpressure drains. Check ws.getBufferedAmount(). Use this to guide / drive your backpressure throttling.

- Defined in index.d.ts:302

### `Optional` dropped

dropped?: (
ws: WebSocket<UserData>,
message: ArrayBuffer,
isBinary: boolean,
) => void | Promise<void>

Handler for a dropped WebSocket message. Messages can be dropped due to specified backpressure settings. Messages are given as ArrayBuffer no matter if they are binary or not. Given ArrayBuffer is valid during the lifetime of this callback (until first await or return) and will be neutered.

- Defined in index.d.ts:300

### `Optional` idleTimeout

idleTimeout?: number

Maximum amount of seconds that may pass without sending or getting a message. Connection is closed if this timeout passes. Resolution (granularity) for timeouts are typically 4 seconds, rounded to closest. Disable by using 0. Defaults to 120.

- Defined in index.d.ts:284

### `Optional` maxBackpressure

maxBackpressure?: number

Maximum length of allowed backpressure per socket when publishing or sending messages. Slow receivers with too high backpressure will be skipped until they catch up or timeout. Defaults to 64 * 1024.

- Defined in index.d.ts:288

### `Optional` maxLifetime

maxLifetime?: number

Maximum number of minutes a WebSocket may be connected before being closed by the server. 0 disables the feature. Valid values are 0 and 1-239.

- Defined in index.d.ts:280

### `Optional` maxPayloadLength

maxPayloadLength?: number

Maximum length of received message. If a client tries to send you a message larger than this, the connection is immediately closed. Defaults to 16 * 1024.

- Defined in index.d.ts:276

### `Optional` message

message?: (
ws: WebSocket<UserData>,
message: ArrayBuffer,
isBinary: boolean,
) => void | Promise<void>

Handler for a WebSocket message. Messages are given as ArrayBuffer no matter if they are binary or not. Given ArrayBuffer is valid during the lifetime of this callback (until first await or return) and will be neutered.

- Defined in index.d.ts:298

### `Optional` open

open?: (ws: WebSocket<UserData>) => void | Promise<void>

Handler for new WebSocket connection. WebSocket is valid from open to close, no errors.

- Defined in index.d.ts:296

### `Optional` ping

ping?: (ws: WebSocket<UserData>, message: ArrayBuffer) => void

Handler for received ping control message. You do not need to handle this, pong messages are automatically sent as per the standard.

- Defined in index.d.ts:306

### `Optional` pong

pong?: (ws: WebSocket<UserData>, message: ArrayBuffer) => void

Handler for received pong control message.

- Defined in index.d.ts:308

### `Optional` sendPingsAutomatically

sendPingsAutomatically?: boolean

Whether or not we should automatically send pings to uphold a stable connection given whatever idleTimeout.

- Defined in index.d.ts:290

### `Optional` subscription

subscription?: (
ws: WebSocket<UserData>,
topic: ArrayBuffer,
newCount: number,
oldCount: number,
) => void

Handler for subscription changes.

- Defined in index.d.ts:310

### `Optional` upgrade

upgrade?: (
res: HttpResponse,
req: HttpRequest,
context: us_socket_context_t,
) => void | Promise<void>

Upgrade handler used to intercept HTTP upgrade requests and potentially upgrade to WebSocket. See UpgradeAsync and UpgradeSync example files.

- Defined in index.d.ts:294