@tauri-apps/plugin-websocket
Esta página aún no está disponible en tu idioma.
Open a WebSocket connection using a Rust client in JS.
Classes
Section titled “Classes”default
Section titled “default”Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L86
A WebSocket connection, created with WebSocket.connect.
2.0.0
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new default(id, listeners): default;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L107
Creates a WebSocket wrapper around an already-open connection.
This is used internally by WebSocket.connect; use that instead of calling this constructor directly.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
id |
number |
The identifier of the connection returned by the Rust side. |
listeners |
Set<(arg) => void> |
The set of callbacks to notify when a message is received. |
Returns
Section titled “Returns”Example
Section titled “Example”import WebSocket from '@tauri-apps/plugin-websocket';
// internally used by `WebSocket.connect`; prefer calling that insteadconst ws = await WebSocket.connect('wss://example.com');Properties
Section titled “Properties”| Property | Type | Description | Defined in |
|---|---|---|---|
id |
number |
The identifier of the underlying connection managed by the Rust side. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L88 |
Methods
Section titled “Methods”addListener()
Section titled “addListener()”addListener(cb): () => void;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L165
Adds a listener that is called whenever a message is received on this connection, including
an error message (as a 'Close' message) when the underlying stream fails.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
cb |
(arg) => void |
The callback invoked with each received Message. |
Returns
Section titled “Returns”A function that removes the listener when called.
() => void
Example
Section titled “Example”import WebSocket from '@tauri-apps/plugin-websocket';
const ws = await WebSocket.connect('wss://example.com');const unlisten = ws.addListener((message) => console.log(message));disconnect()
Section titled “disconnect()”disconnect(): Promise<void>;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L218
Closes the WebSocket connection, sending a normal closure (1000) close frame to the server.
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”import WebSocket from '@tauri-apps/plugin-websocket';
const ws = await WebSocket.connect('wss://example.com');await ws.disconnect();send()
Section titled “send()”send(message): Promise<void>;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L189
Sends a message through the WebSocket connection.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
message |
| string | number[] | Message |
The message to send: a plain string (sent as a 'Text' message), a numeric array (sent as a 'Binary' message), or an explicit Message object. |
Returns
Section titled “Returns”Promise<void>
A promise resolving when the message has been sent.
Example
Section titled “Example”import WebSocket from '@tauri-apps/plugin-websocket';
const ws = await WebSocket.connect('wss://example.com');await ws.send('Hello World');await ws.send([1, 2, 3]);await ws.send({ type: 'Text', data: 'Hello World' });connect()
Section titled “connect()”static connect(url, config?): Promise<default>;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L127
Opens a WebSocket connection to the given URL.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
url |
string |
The URL to connect to, e.g. ws:// or wss:// (the latter requires one of the plugin’s TLS Cargo features to be enabled). |
config? |
ConnectionConfig |
Configuration forwarded to the underlying tungstenite client. |
Returns
Section titled “Returns”A promise resolving to the connected WebSocket.
Example
Section titled “Example”import WebSocket from '@tauri-apps/plugin-websocket';
const ws = await WebSocket.connect('wss://example.com');2.0.0
Interfaces
Section titled “Interfaces”CloseFrame
Section titled “CloseFrame”Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L64
The code and reason sent (or received) when a WebSocket connection is closed.
Properties
Section titled “Properties”| Property | Type | Description | Defined in |
|---|---|---|---|
code |
number |
The WebSocket close status code, e.g. 1000 for a normal closure. |
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L66 |
reason |
string |
A human-readable explanation for why the connection was closed. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L68 |
ConnectionConfig
Section titled “ConnectionConfig”Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L16
Configuration used to open a WebSocket connection, forwarded to the underlying tungstenite client.
Properties
Section titled “Properties”| Property | Type | Description | Defined in |
|---|---|---|---|
acceptUnmaskedFrames? |
boolean |
When set to true, the server will accept and handle unmasked frames from the client. According to the RFC 6455, the server must close the connection to the client in such cases, however it seems like there are some popular libraries that are sending unmasked frames, ignoring the RFC. By default this option is set to false, i.e. according to RFC 6455. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L44 |
headers? |
HeadersInit |
Additional connect request headers. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L48 |
maxFrameSize? |
number | "none" |
The maximum size of a single incoming message frame. The string “none” means no size limit. The limit is for frame payload NOT including the frame header. The default value is 16 MiB which should be reasonably big for all normal use-cases but small enough to prevent memory eating by a malicious user. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L40 |
maxMessageSize? |
number | "none" |
The maximum size of an incoming message. The string “none” means no size limit. The default value is 64 MiB which should be reasonably big for all normal use-cases but small enough to prevent memory eating by a malicious user. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L36 |
maxWriteBufferSize? |
number |
The max size of the write buffer in bytes. Setting this can provide backpressure in the case the write buffer is filling up due to write errors. The default value is unlimited. Note: The write buffer only builds up past write_buffer_size when writes to the underlying stream are failing. So the write buffer can not fill up if you are not observing write errors. Note: Should always be at least write_buffer_size + 1 message and probably a little more depending on error handling strategy. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L32 |
readBufferSize? |
number |
Read buffer capacity. The default value is 128 KiB. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L20 |
writeBufferSize? |
number |
The target minimum size of the write buffer to reach before writing the data to the underlying stream. The default value is 128 KiB. If set to 0 each message will be eagerly written to the underlying stream. It is often more optimal to allow them to buffer a little, hence the default value. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L25 |
MessageKind
Section titled “MessageKind”Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L54
A tagged WebSocket message, discriminated by its type field.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
D |
Properties
Section titled “Properties”| Property | Type | Description | Defined in |
|---|---|---|---|
data |
D |
The message payload, whose shape depends on MessageKind.type. | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L58 |
type |
T |
The kind of message, e.g. 'Text', 'Binary', 'Ping', 'Pong' or 'Close'. |
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L56 |
Type Aliases
Section titled “Type Aliases”Message
Section titled “Message”type Message = | MessageKind<"Text", string> | MessageKind<"Binary", number[]> | MessageKind<"Ping", number[]> | MessageKind<"Pong", number[]>| MessageKind<"Close", CloseFrame | null>;Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/websocket/guest-js/index.ts#L74
A message sent to or received from a WebSocket connection.
© 2026 Tauri Contributors. CC-BY / MIT