tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
853 stars 238 forks source link

[feat] provide SSE (ServerSentEvents) as alternative to websocket #1002

Open nimo23 opened 7 months ago

nimo23 commented 7 months ago

Describe the problem

Currently we need to use @tauriApps/plugin-websocket for unidirectional streaming (from server to client). However, there is another web standard specifically for such unidirectional streaming: Server Sent Events (SSE).

Describe the solution you'd like

Would be nice to also have @tauriApps/plugin-sse to support "server sent events".

Especially for mobile applications, SSE is a much more energy-saving alternative. Btw, in contrast to the use of the "web's native implementation", Tauri can even improve SSE communication by

Alternatives considered

No response

Additional context

No response

lloydzhou commented 17 hours ago

here is stream_fetch command for tauri rust code: stream.rs

mod stream;
fn main() {
  tauri::Builder::default()
    .invoke_handler(tauri::generate_handler![stream::stream_fetch])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

js code: stream.ts

import { fetch as tauriStreamFetch } from "./stream"
import { fetchEventSource } from '@microsoft/fetch-event-source';

await fetchEventSource('/api/sse', {
    fetch: tauriStreamFetch,
    onmessage(ev) {
        console.log(ev.data);
    }
});