Skip to content

@tauri-apps/plugin-websocket

Open a WebSocket connection using a Rust client in JS.

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

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.

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.

default

import WebSocket from '@tauri-apps/plugin-websocket';
// internally used by `WebSocket.connect`; prefer calling that instead
const ws = await WebSocket.connect('wss://example.com');
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

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.

Parameter Type Description
cb (arg) => void The callback invoked with each received Message.

A function that removes the listener when called.

() => void

import WebSocket from '@tauri-apps/plugin-websocket';
const ws = await WebSocket.connect('wss://example.com');
const unlisten = ws.addListener((message) => console.log(message));

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.

Promise<void>

import WebSocket from '@tauri-apps/plugin-websocket';
const ws = await WebSocket.connect('wss://example.com');
await ws.disconnect();

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.

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.

Promise<void>

A promise resolving when the message has been sent.

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' });

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.

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.

Promise<default>

A promise resolving to the connected WebSocket.

import WebSocket from '@tauri-apps/plugin-websocket';
const ws = await WebSocket.connect('wss://example.com');

2.0.0

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.

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

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.

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

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 Parameter
T
D
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 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