Interface WebSocketBehavior<UserData>

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

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

Type Parameters

  • UserData

Properties

close?: ((ws, code, message) => 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.

Type declaration

    • (ws, code, message): 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.

      Parameters

      Returns void

closeOnBackpressureLimit?: number

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

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.

drain?: ((ws) => void)

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

Type declaration

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

      Parameters

      Returns void

dropped?: ((ws, message, isBinary) => 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.

Type declaration

    • (ws, message, isBinary): 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.

      Parameters

      Returns void | Promise<void>

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.

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.

maxLifetime?: number

Maximum number of minutes a WebSocket may be connected before being closed by the server. 0 disables the feature.

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.

message?: ((ws, message, isBinary) => 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.

Type declaration

    • (ws, message, isBinary): 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.

      Parameters

      Returns void | Promise<void>

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

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

Type declaration

    • (ws): void | Promise<void>
    • Handler for new WebSocket connection. WebSocket is valid from open to close, no errors.

      Parameters

      Returns void | Promise<void>

ping?: ((ws, message) => void)

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

Type declaration

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

      Parameters

      Returns void

pong?: ((ws, message) => void)

Handler for received pong control message.

Type declaration

    • (ws, message): void
    • Handler for received pong control message.

      Parameters

      Returns void

sendPingsAutomatically?: boolean

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

subscription?: ((ws, topic, newCount, oldCount) => void)

Handler for subscription changes.

Type declaration

    • (ws, topic, newCount, oldCount): void
    • Handler for subscription changes.

      Parameters

      Returns void

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

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

Type declaration

    • (res, req, context): void | Promise<void>
    • Upgrade handler used to intercept HTTP upgrade requests and potentially upgrade to WebSocket. See UpgradeAsync and UpgradeSync example files.

      Parameters

      Returns void | Promise<void>

Generated using TypeDoc