tauri-plugin-socketio 0.2.0

Tauri 2 plugin: named Socket.IO v4 clients (socketio-next) shared by Rust and the webview
Documentation

Crates.io npm docs.rs Guide CI License Donate

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
[dependencies]
tauri-plugin-socketio = "0.1"
pnpm add tauri-plugin-socketio-api   # or npm / yarn
// src-tauri/src/lib.rs
tauri::Builder::default()
    .plugin(tauri_plugin_socketio::init())
    // 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
{ "identifier": "default", "windows": ["main"], "permissions": ["core:default", "socketio:default"] }

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 tauri_plugin_socketio::socketio_next::ClientBuilder;
use tauri_plugin_socketio::{Access, SocketIoExt};

let io = app.socketio();
io.reserve("core"); // in setup, before any await: webviews cannot take the id
let chat = io.connect("chat", ClientBuilder::new("https://chat.example.com")).await?;
io.insert("core", my_client, Access::ReadOnly)?; // 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