macotron.bonjour
Browse the local network for Bonjour services, such as AirPlay receivers, printers, and file shares, and get each one's host, port, and TXT record.
// List AirPlay receivers on the network.
const found = await macotron.bonjour.browse("_airplay._tcp", { timeout: 2 });
macotron.notify.toast("AirPlay", found.map((s) => s.name).join(", ") || "None found");
Browsing
browse(type: string, opts?: { timeout?: number }): Promise<Array<{ name, type, host, port, txt }>>
Searches the local. domain for services of one type, such as "_airplay._tcp", "_ipp._tcp", or "_smb._tcp". A trailing .local. on the type is allowed and removed.
The search runs for the whole timeout, in seconds (default 1.5), then resolves with every service that resolved in that time. Each row has:
name: the service name people see, such as"Living Room".type: the service type, without.local..host: the host name, such as"Living-Room.local.".port: the port number.txt: the TXT record as an object of strings. A value that is not UTF-8 text is base64.
A service that does not resolve to a host and port before the timeout is left out, so a longer timeout finds slower devices. Nothing found resolves to an empty array.
macOS asks for Local Network permission the first time Macotron browses. Without it, browse finds nothing.
See also
- macotron.appletv, which uses Bonjour to find Apple TVs.
- macotron.udp to talk to a device once you know its host and port.
- macotron.network for Wi-Fi and interfaces.