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
yarn run tauri add websocket
pnpm tauri add websocket
bun tauri add websocket
cargo tauri add websocket
-
Install the websocket plugin by adding the following to your
Cargo.toml
file:src-tauri/Cargo.toml [dependencies]tauri-plugin-websocket = "2.0.0-beta"# alternatively with Git:tauri-plugin-websocket = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } -
Modify
lib.rs
to initialize the plugin:src-tauri/src/lib.rs fn run() {tauri::Builder::default().plugin(tauri_plugin_websocket::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm install @tauri-apps/plugin-websocketyarn add @tauri-apps/plugin-websocketpnpm add @tauri-apps/plugin-websocketbun add @tauri-apps/plugin-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.
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": ["websocket:default"]}
Permission | Description |
---|---|
websocket:default | Allows connecting and sending data to a WebSocket server |
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