Skip to content
Tauri

Websocket

Open a WebSocket connection using a Rust client in JavaScript.

Supported Platforms

  • Windows
  • Linux
  • macOS

Setup

This plugin requires a Rust version of at least 1.75

Install the websocket plugin to get started.

Use your project’s package manager to add the dependency:

npm run tauri add websocket

Usage

The websocket plugin is available in JavaScript.

import WebSocket from '@tauri-apps/plugin-websocket';
const ws = await WebSocket.connect('ws://127.0.0.1:8080');
ws.addListener((msg) => {
console.log('Received Message:', msg);
});
await ws.send('Hello World!');
await ws.disconnect();

Permissions

By default all plugin commands are blocked and cannot be accessed. You must define a list of permissions in your capabilities configuration.

See Permissions Overview for more information.

src-tauri/capabilities/main.json
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "main-capability",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": ["websocket:default"]
}
PermissionDescription
websocket:defaultAllows connecting and sending data to a WebSocket server
websocket:allow-connectEnables the connect command without any pre-configured scope.
websocket:deny-connectDenies the connect command without any pre-configured scope.
websocket:allow-sendEnables the send command without any pre-configured scope.
websocket:deny-sendDenies the send command without any pre-configured scope.

© 2024 Tauri Contributors. CC-BY / MIT