svelte-broadcast Svelte Themes

Svelte Broadcast

Full-stack web socket based broadcasting library for Svelte.

Svelte Broadcast

Svelte Broadcast is a full-stack websocket-based broadcasting library.

How to use

Set up server

At root of routes directory.

+layout.server.ts

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.
        })
    }
}

+page.server.ts

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.

+page.ts

export let ssr = false;

Top categories

svelte logo

Need a Svelte website built?

Hire a professional Svelte developer today.
Loading Svelte Themes