A WebSocket connection that is valid from open to close event. Read more about this in the user manual.

interface WebSocket<UserData> {
    close(): void;
    cork(cb): WebSocket<UserData>;
    end(code?, shortMessage?): void;
    getBufferedAmount(): number;
    getRemoteAddress(): ArrayBuffer;
    getRemoteAddressAsText(): ArrayBuffer;
    getTopics(): string[];
    getUserData(): UserData;
    isSubscribed(topic): boolean;
    ping(message?): number;
    publish(topic, message, isBinary?, compress?): boolean;
    send(message, isBinary?, compress?): number;
    subscribe(topic): boolean;
    unsubscribe(topic): boolean;
}

Type Parameters

  • UserData

Methods

  • Forcefully closes this WebSocket. Immediately calls the close handler. No WebSocket close message is sent.

    Returns void

  • See HttpResponse.cork. Takes a function in which the socket is corked (packing many sends into one single syscall/SSL block)

    Parameters

    • cb: (() => void)
        • (): void
        • Returns void

    Returns WebSocket<UserData>

  • Gracefully closes this WebSocket. Immediately calls the close handler. A WebSocket close message is sent with code and shortMessage.

    Parameters

    Returns void

  • Returns the bytes buffered in backpressure. This is similar to the bufferedAmount property in the browser counterpart. Check backpressure example.

    Returns number

  • Returns the remote IP address. Note that the returned IP is binary, not text.

    IPv4 is 4 byte long and can be converted to text by printing every byte as a digit between 0 and 255. IPv6 is 16 byte long and can be converted to text in similar ways, but you typically print digits in HEX.

    See getRemoteAddressAsText() for a text version.

    Returns ArrayBuffer

  • Returns the remote IP address as text. See RecognizedString.

    Returns ArrayBuffer

  • Returns a list of topics this websocket is subscribed to.

    Returns string[]

  • Returns whether this websocket is subscribed to topic.

    Parameters

    Returns boolean

  • Sends a ping control message. Returns sendStatus similar to WebSocket.send (regarding backpressure). This helper function correlates to WebSocket::send(message, uWS::OpCode::PING, ...) in C++.

    Parameters

    Returns number

  • Publish a message under topic. Backpressure is managed according to maxBackpressure, closeOnBackpressureLimit settings. Order is guaranteed since v20.

    Parameters

    Returns boolean

  • Sends a message. Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure that will drain over time. You can check backpressure before or after sending by calling getBufferedAmount().

    Make sure you properly understand the concept of backpressure. Check the backpressure example file.

    Parameters

    Returns number

  • Unsubscribe from a topic. Returns true on success, if the WebSocket was subscribed.

    Parameters

    Returns boolean

Generated using TypeDoc