tauri-apps / tauri-apps/plugins-workspace
[websocket] WebSocket subprotocol negotiation fails when using `Sec-WebSocket-Protocol` header
- Dominant language
- Rust
- Stars
- 1.8k
- Forks
- 602
- Avg merge
- 4d 14h
- Merged PRs (30d)
- 9
Description
**Describe the bug**
When configuring the `Sec-WebSocket-Protocol` header in WebSocket connections using tauri-plugin-websocket, I encounter the error:
`WebSocket protocol error: SubProtocol error: Server sent no subprotocol`.
This occurs despite the same setup working correctly with Python's websockets library.
**To Reproduce**
Steps to reproduce the behavior:
1. Set up a simple WebSocket server (Python example provided below)
2. Attempt to connect using tauri-plugin-websocket with `Sec-WebSocket-Protocol` header
3. Observe the protocol error
Client code (TypeScript):
```ts
import WebSocket from '@tauri-apps/plugin-websocket';
const ws = await WebSocket.connect('ws://127.0.0.1:8080', {
headers: {
'Sec-WebSocket-Protocol': 'test'
}
});
```
Server code (Python):
```py
import asyncio
import websockets
async def echo(websocket):
async for message in websocket:
print(f"Received message: {message}")
await websocket.send(f"Echo: {message}")
async def main():
async with websockets.serve(echo, "localhost", port=8080):
print("WebSocket server started, listening on port 8080...")
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())
```
**Expected behavior**
The connection should establish successfully when specifying `Sec-WebSocket-Protocol`, similar to how it works with Python's websockets library:
```py
import asyncio
import websockets
async def ws(url, headers):
async with websockets.connect(url, additional_headers=headers) as websocket:
await websocket.send("Hello World!")
# ... rest of implementation ...
custom_headers = {
"Sec-WebSocket-Protocol": "test",
}
if __name__ == "__main__":
asyncio.run(ws("ws://127.0.0.1:8080", headers=custom_headers))
```
**Additional context**
```
[✔] Environment
- OS: Windows 10.0.26100 x86_64 (X64)
✔ WebView2: 136.0.3240.92
✔ MSVC: Visual Studio Community 2022
✔ rustc: 1.85.0-nightly (6b6a867ae 2024-11-27)
✔ cargo: 1.85.0-nightly (4c39aaff6 2024-11-25)
✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
✔ Rust toolchain: nightly-x86_64-pc-windows-msvc (default)
- node: 23.5.0
- pnpm: 9.14.2
- npm: 10.9.2
- bun: 1.2.12
- deno: deno 2.1.4
[-] Packages
- tauri 🦀: 2.5.1
- tauri-build 🦀: 2.2.0
- wry 🦀: 0.51.2
- tao 🦀: 0.33.0
- @tauri-apps/api : 2.5.0
- @tauri-apps/cli : 2.5.0
[-] Plugins
- tauri-plugin-websocket 🦀: 2.3.0
- @tauri-apps/plugin-websocket : 2.3.0
[-] App
- build-type: bundle
- CSP: unset
- frontendDist: ../dist
- devUrl: http://localhost:1420/
- bundler: Vite
```
Contributor guide
Assessment
This issue has not been assessed yet.