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';
// when using `"withGlobalTauri": true`, you may use
// const WebSocket = window.__TAURI_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 potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.

See the Capabilities Overview for more information and the step by step guide to use plugin permissions.

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"]
}

Default Permission

Allows connecting and sending data to a WebSocket server

  • allow-connect
  • allow-send

Permission Table

Identifier Description

websocket:allow-connect

Enables the connect command without any pre-configured scope.

websocket:deny-connect

Denies the connect command without any pre-configured scope.

websocket:allow-send

Enables the send command without any pre-configured scope.

websocket:deny-send

Denies the send command without any pre-configured scope.


© 2024 Tauri Contributors. CC-BY / MIT