Svelte Broadcast is a full-stack websocket-based broadcasting library.
At root of routes directory.
import BroadcastServer from "$lib/broadcast-server.js";
import type { BroadcastReceiveArgs } from "$lib/types.js";
export function load() {
const ss = BroadcastServer.instance;
if (!ss.hasReceiving()) {
ss.setReceiving((args: BroadcastReceiveArgs) => {
// put your function.
})
}
}
import BroadcastClient from "$lib/broadcast-client.js";
const sc = new BroadcastClient('ws://localhost:8080');
sc.on('open', (e) => console.log('opened!'));
sc.on('message', (data) => console.log(data));
⚠️ BroadcastClient is used WebSocket API on browser. Therefore, you can use only client mode. If you don't use SSR, disable it like this.
export let ssr = false;