tauri-apps / tauri-apps/plugins-workspace

Tauri WebSocket plugin can’t listen to messages right after a connection is established.

Open
#3,152 3 comments 2 reactions 0 assignees View on GitHub
plugin: websocket type: bug
Dominant language
Rust
Stars
1.8k
Forks
602
Avg merge
4d 14h
Merged PRs (30d)
9

Description

### Describe the bug

I tried porting socket.io to work with Tauri WebSocket and Fetch, but I found that the WebSocket connections were unstable.
After digging into the issue, I discovered that Tauri’s WebSocket JS API currently has a chance of failing to capture the very first message sent by the server right after the connection is established.

The code looks roughly like this:

```javascript
import RawWebSocket, { CloseFrame } from '@tauri-apps/plugin-websocket'

void RawWebSocket.connect(uri, {
headers: opts.headers,
}).then((ws) => {
this.engine = ws
this.onopen?.()
this.engine.addListener((message) => {
console.log('WebSocket message received:', message);
if (message.type === 'Close') {
this.onclose?.(message.data)
return
}

if (message.type === 'Ping' || message.type === 'Pong') {
return
}

const data = message.type === 'Binary' ? (() => {
if (this.binaryType === 'arraybuffer') {
return new Uint8Array(message.data).buffer
}
return new Blob([new Uint8Array(message.data)])
})() : message.data

this.onmessage?.({ data })
})
}).catch(this.onerror);
```

The problem is that you can only call `addListener` to listen for incoming messages after the connect call returns and you obtain the `WebSocket` instance. If the server sends a message during this gap, that message gets lost.

As a result, socket.io cannot complete its handshake properly. This issue is especially easy to reproduce on Windows.

### Reproduction

_No response_

### Expected behavior

It should provide a way to listen to messages right from the moment the connection is established.

### Full `tauri info` output

```text
[✔] Environment
- OS: Windows 10.0.26100 x86_64 (X64)
✔ WebView2: 142.0.3595.94
✔ MSVC: Visual Studio Community 2022
✔ rustc: 1.90.0 (1159e78c4 2025-09-14)
✔ cargo: 1.90.0 (840b83a10 2025-07-30)
✔ rustup: 1.28.2 (e4f3ad6f8 2025-04-28)
✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
- node: 22.20.0
- npm: 10.9.3

[-] Packages
- tauri 🦀: 2.8.5, (outdated, latest: 2.9.4)
- tauri-build 🦀: 2.4.1, (outdated, latest: 2.5.3)
- wry 🦀: 0.53.3, (outdated, latest: 0.53.5)
- tao 🦀: 0.34.3, (outdated, latest: 0.34.5)
- @tauri-apps/api : 2.8.0 (outdated, latest: 2.9.1)
- @tauri-apps/cli : 2.8.4 (outdated, latest: 2.9.5)

[-] Plugins
- tauri-plugin-http 🦀: 2.5.2, (outdated, latest: 2.5.4)
- @tauri-apps/plugin-http : not installed!
- tauri-plugin-dialog 🦀: 2.4.0, (outdated, latest: 2.4.2)
- @tauri-apps/plugin-dialog : 2.4.2
- tauri-plugin-log 🦀: 2.7.1
- @tauri-apps/plugin-log : 2.7.1
- tauri-plugin-updater 🦀: 2.9.0
- @tauri-apps/plugin-updater : 2.9.0
- tauri-plugin-store 🦀: 2.4.1
- @tauri-apps/plugin-store : 2.4.1
- tauri-plugin-fs 🦀: 2.4.2, (outdated, latest: 2.4.4)
- @tauri-apps/plugin-fs : not installed!
- tauri-plugin-opener 🦀: 2.5.0, (outdated, latest: 2.5.2)
- @tauri-apps/plugin-opener : 2.5.0 (outdated, latest: 2.5.2)
- tauri-plugin-websocket 🦀: 2.4.1
- @tauri-apps/plugin-websocket : not installed!
- tauri-plugin-notification 🦀: 2.3.3
- @tauri-apps/plugin-notification : 2.3.3

[-] App
- build-type: bundle
- CSP: unset
- frontendDist: ../dist
- devUrl: http://localhost:1420/
- framework: Vue.js
- bundler: Vite
```

### Stack trace

```text

```

### Additional context

Maybe we can add listeners to `ConnectionConfig`?

I can send a PR to this issue if needed.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.