# Interface WebSocket<UserData>

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: () => void): WebSocket<UserData>;
end(code?: number, shortMessage?: RecognizedString): void;
getBufferedAmount(): number;
getRemoteAddress(): ArrayBuffer;
getRemoteAddressAsText(): ArrayBuffer;
getRemotePort(): number;
getTopics(): string[];
getUserData(): UserData;
isSubscribed(topic: RecognizedString): boolean;
ping(message?: RecognizedString): number;
publish(
topic: RecognizedString,
message: RecognizedString,
isBinary?: boolean,
compress?: boolean,
): boolean;
send(
message: RecognizedString,
isBinary?: boolean,
compress?: boolean,
): number;
sendFirstFragment(
message: RecognizedString,
isBinary?: boolean,
compress?: boolean,
): number;
sendFragment(message: RecognizedString, compress?: boolean): number;
sendLastFragment(message: RecognizedString, compress?: boolean): number;
subscribe(topic: RecognizedString): boolean;
unsubscribe(topic: RecognizedString): boolean;
}

#### Type Parameters

- UserData

- Defined in index.d.ts:58

##### Index

### Methods

close
cork
end
getBufferedAmount
getRemoteAddress
getRemoteAddressAsText
getRemotePort
getTopics
getUserData
isSubscribed
ping
publish
send
sendFirstFragment
sendFragment
sendLastFragment
subscribe
unsubscribe

## Methods

### close

- close(): void

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

  #### Returns void

  - Defined in index.d.ts:78

### cork

- cork(cb: () => void): WebSocket<UserData>

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

  #### Parameters

  - cb: () => void

  #### Returns WebSocket<UserData>

  - Defined in index.d.ts:101

### end

- end(code?: number, shortMessage?: RecognizedString): void

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

  #### Parameters

  - `Optional` code: number
  - `Optional` shortMessage: RecognizedString

  #### Returns void

  - Defined in index.d.ts:73

### getBufferedAmount

- getBufferedAmount(): number

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

  #### Returns number

  - Defined in index.d.ts:68

### getRemoteAddress

- getRemoteAddress(): ArrayBuffer

  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

  - Defined in index.d.ts:110

### getRemoteAddressAsText

- getRemoteAddressAsText(): ArrayBuffer

  Returns the remote IP address as text. See RecognizedString.

  #### Returns ArrayBuffer

  - Defined in index.d.ts:113

### getRemotePort

- getRemotePort(): number

  Returns the remote port number.

  #### Returns number

  - Defined in index.d.ts:116

### getTopics

- getTopics(): string[]

  Returns a list of topics this websocket is subscribed to.

  #### Returns string[]

  - Defined in index.d.ts:93

### getUserData

- getUserData(): UserData

  Returns the UserData object.

  #### Returns UserData

  - Defined in index.d.ts:119

### isSubscribed

- isSubscribed(topic: RecognizedString): boolean

  Returns whether this websocket is subscribed to topic.

  #### Parameters

  - topic: RecognizedString

  #### Returns boolean

  - Defined in index.d.ts:90

### ping

- ping(message?: RecognizedString): number

  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

  - `Optional` message: RecognizedString

  #### Returns number

  - Defined in index.d.ts:81

### publish

- publish(
  topic: RecognizedString,
  message: RecognizedString,
  isBinary?: boolean,
  compress?: boolean,
  ): boolean

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

  #### Parameters

  - topic: RecognizedString
  - message: RecognizedString
  - `Optional` isBinary: boolean
  - `Optional` compress: boolean

  #### Returns boolean

  - Defined in index.d.ts:98

### send

- send(message: RecognizedString, isBinary?: boolean, compress?: boolean): number

  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

  - message: RecognizedString
  - `Optional` isBinary: boolean
  - `Optional` compress: boolean

  #### Returns number

  - Defined in index.d.ts:63

### sendFirstFragment

- sendFirstFragment(
  message: RecognizedString,
  isBinary?: boolean,
  compress?: boolean,
  ): number

  Sends the first fragment of a fragmented message. Use for sending large messages in chunks. Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure.

  #### Parameters

  - message: RecognizedString
  - `Optional` isBinary: boolean
  - `Optional` compress: boolean

  #### Returns number

  - Defined in index.d.ts:124

### sendFragment

- sendFragment(message: RecognizedString, compress?: boolean): number

  Sends a middle fragment of a fragmented message. Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure.

  #### Parameters

  - message: RecognizedString
  - `Optional` compress: boolean

  #### Returns number

  - Defined in index.d.ts:129

### sendLastFragment

- sendLastFragment(message: RecognizedString, compress?: boolean): number

  Sends the last fragment of a fragmented message. Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure.

  #### Parameters

  - message: RecognizedString
  - `Optional` compress: boolean

  #### Returns number

  - Defined in index.d.ts:134

### subscribe

- subscribe(topic: RecognizedString): boolean

  Subscribe to a topic.

  #### Parameters

  - topic: RecognizedString

  #### Returns boolean

  - Defined in index.d.ts:84

### unsubscribe

- unsubscribe(topic: RecognizedString): boolean

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

  #### Parameters

  - topic: RecognizedString

  #### Returns boolean

  - Defined in index.d.ts:87