tauri-plugin-socketio
A Tauri 2 plugin for Socket.IO v4 on top of socketio-next: one registry of named clients that Rust and the webviews share. Rust connects clients or registers its own, optionally read-only for the webview; JS connects or attaches with an API close to socket.io-client. The socket runs in Rust: no CORS, no browser websocket limits, and the connection survives page reloads.
Documentation: s00d.github.io/rust-socketio/tauri/setup
Install
# src-tauri/Cargo.toml
[]
= "0.1"
// src-tauri/src/lib.rs
default
.plugin
// or restrict the webview's targets:
// .plugin(tauri_plugin_socketio::Builder::new().allow_url_prefix("https://api.example.com").build())
// src-tauri/capabilities/default.json
socketio:default allows every command, socketio:allow-read only observes
(permissions).
JS
import { connect, attach } from 'tauri-plugin-socketio-api'
const socket = await connect('chat', { url: 'https://chat.example.com', auth: { token } })
socket.on('message', (text, from) => console.log(from, text))
socket.on('question', (q, ack) => ack('answer'))
await socket.emit('message', 'hi')
const [sum] = await socket.emitWithAck('add', [1, 2], { timeout: 5000 })
const core = await attach('core') // a client Rust registered
Rust
use ClientBuilder;
use ;
let io = app.socketio;
io.reserve; // in setup, before any await: webviews cannot take the id
let chat = io.connect.await?;
io.insert?; // the webview may watch, not emit
Before shipping, read the security notes: reserved ids, read-only clients, the URL allow-list (the CSP does not cover these sockets) and refused headers.
Support
If the plugin helps you, you can support it via DonationAlerts. Bugs and ideas: issues.
License
MIT