Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Find apps (build a hub)

Your app is a hub when it connects to other apps: a device player, a script tool. The hub listens on the first free port from 37420 to 37429 and tells you when a usable app appears, changes or leaves.

Start a hub

import { startHub } from "funlink";

const hub = await startHub({
  name: "My Player",
  version: "1.0.0",
  supports: ["buttplug-ws", "tcode-ws"],
  onEvent: (event) => {
    if (event.type === "found" && event.peer.state === "available") offer(event.peer);
    if (event.type === "lost") withdraw(event.peer);
  },
});

// On exit:
await hub.close();
#![allow(unused)]
fn main() {
use funlink::{Hub, HubEvent, PeerState, Protocol};

let hub = Hub::builder("My Player")
    .version("1.0.0")
    .supports([Protocol::ButtplugWs, Protocol::TcodeWs])
    .start(|event| match event {
        HubEvent::Found(peer) if peer.state == PeerState::Available => offer(peer),
        HubEvent::Lost { peer, .. } => withdraw(peer),
        _ => {}
    })?;
// Dropping `hub` stops it.
}
using Funlink;

await using var hub = await FunlinkHub.StartAsync(
    new HubOptions("My Player", [Protocol.ButtplugWs, Protocol.TcodeWs]) { Version = "1.0.0" },
    e =>
    {
        switch (e)
        {
            case PeerFound { Peer.State: PeerState.Available } found: Offer(found.Peer); break;
            case PeerLost lost: Withdraw(lost.Peer); break;
        }
    });
try (var hub = Funlink.hub(
        HubOptions.of("My Player", Protocol.BUTTPLUG_WS, Protocol.TCODE_WS).withVersion("1.0.0"),
        event -> {
            if (event instanceof HubEvent.Found found && found.peer().state() == PeerState.AVAILABLE) offer(found.peer());
            if (event instanceof HubEvent.Lost lost) withdraw(lost.peer());
        })) {
    // Run your app.
}

The C library is a peer only: hubs run on a computer. Use one of the four libraries there.

The Godot add-on is a peer only. A Godot .NET project can use the C# library, which also runs hubs.

Callbacks run on a background thread (Rust, C#, Java) or the event loop (TypeScript). Hand work to your UI thread as usual.

Events

EventWhen
foundAn app announced itself for the first time, or after a restart.
updatedIts name, services, icon or state changed.
lostIt left: bye, expired (15 s without announce) or restarted (new session; a found follows).

Each event carries a Peer: the announced record (id, name, version, icon, services, ext) plus:

  • usable: the announced services your hub supports, in announced order. Connect to usable[0].
  • state: available, connected, dismissed or unsupported (nothing usable).
  • origin: the web page’s origin, only when you allow browsers.

Ask the user, then connect

A hub must not connect just because an app announced itself. Show a prompt, then record the user’s answer:

async function onAnswer(peer: Peer, answer: "connect" | "dismiss") {
  if (answer === "dismiss") return hub.setState(peer, "dismissed");
  await connect(peer.usable[0].endpoint);   // your existing connection code
  hub.setState(peer, "connected");
}
#![allow(unused)]
fn main() {
if accepted {
    connect(&peer.usable[0].endpoint)?;   // your existing connection code
    hub.set_state(&peer, PeerState::Connected);
} else {
    hub.set_state(&peer, PeerState::Dismissed);
}
}
if (accepted)
{
    await ConnectAsync(peer.Usable[0].Endpoint);   // your existing connection code
    hub.SetState(peer, PeerState.Connected);
}
else hub.SetState(peer, PeerState.Dismissed);
if (accepted) {
    connect(peer.usable().get(0).endpoint());   // your existing connection code
    hub.setState(peer, PeerState.CONNECTED);
} else {
    hub.setState(peer, PeerState.DISMISSED);
}

The C library is a peer only: hubs run on a computer. Use one of the four libraries there.

The Godot add-on is a peer only. A Godot .NET project can use the C# library, which also runs hubs.

The peer sees the state in its next answer. dismissed lasts until the peer restarts; set available again when you disconnect.

Good prompt behavior, as the specification asks:

  • Prompt at most once per launch of a peer, and not while you are already connected to a source.
  • Offer Always connect and Never ask and store them by peer id (and origin, for browsers).

What the library already does

Validation, Host checks against DNS rebinding, rate limits (200 ms per app, 64 apps), 96 KiB body limit, expiry after 15 s, restart detection and, on Linux, refusing other users’ processes. Web pages are rejected unless you opt in (allowBrowsers / allow_browsers / AllowBrowsers / withBrowsers); then each peer carries its origin and you must show it in the prompt.

Running two hubs

Ten ports allow several hubs at once (for example RustyFunPlayer and your app). Each hub gets every announcement independently. Coordinating which hub uses a service is not part of v1.

Once your hub works, add the “Funlink hub” badge to its page.