# FKN Developer Docs (Full) > Hand-written developer documentation for `@fkn/lib`, `fkn.dev`, and `fkn.app`. For the generated TypeDoc reference, read `https://docs.fkn.dev/llms-api.txt`. --- # FKN Developer Docs URL: https://docs.fkn.dev/ Source: src/content/docs/index.mdx **Looking for help using FKN apps?** Account setup, recovery codes, plans, permissions, and privacy answers live in the [FKN Help Guide](https://www.fkn.app/help/). ## Start with a goal Each link takes one goal to a working result: | Networking | Storage and realtime | Browser automation and publishing | |---|---|---| | [Fetch without CORS headers](/guides/fetch/) | [Save files to the user's account](/guides/storage/) | [Embed a site and drive it](/guides/frames/) | | [Open a TCP or UDP socket](/guides/sockets/) | [Store large encrypted objects](/guides/object-storage/) | [Ask for permissions cleanly](/guides/permissions/) | | [Open a WebSocket through FKN](/guides/websocket/) | [Build an encrypted realtime room](/guides/rooms/) | [Add a sandboxed package](/recipes/use-a-package/) | | [Run sockets in a worker](/recipes/sockets-in-a-worker/) | [Back app state up to the account](/recipes/account-backup/) | [Register and sign releases on fkn.dev](/publishing/) | Guides cover one API surface completely. Recipes combine several calls into a single task. ## Try it live The demo below loads en.wikipedia.org inside an attached frame and restyles it. **Run the demo** reads the heading, types a search query, and clicks search. With the FKN extension installed, one consent sheet covers all three actions. Without it, the same actions run over the FKN cloud with nothing installed: [Interactive demo: Acting on a real site, guided](https://docs.fkn.dev/demos/attach-frame/guided/) ## Browse by surface ### fetch() Standard `fetch` and `Response` sent through the cloud proxy or the user's extension. See [fetch()](/guides/fetch/). ### TCP and UDP sockets Node-compatible `net` and `dgram` sockets carried over WebVPN relays. See [TCP and UDP sockets](/guides/sockets/). ### WebSocket Cross-origin `WebSocket` connections carried over FKN with custom headers. See [WebSocket](/guides/websocket/). ### Storage Node-style `fs` across local OPFS, account-synced cloud files, and hybrid mode. See [storage](/guides/storage/). ### Object storage Encrypted content-addressed blobs for media, attachments, and public files. See [object storage](/guides/object-storage/). ### Rooms End-to-end encrypted realtime rooms for sync, chat, and watch-together features. See [rooms](/guides/rooms/). ### Frames Embed a live website inside your layout and drive it with `attachFrame()`. See [frames](/guides/frames/). ### Locators and actions Playwright-style locators to read text, click, type, and evaluate inside attached frames. See [locators and actions](/guides/locators/). ### Permissions and consent Explicit per-site user consent sheets, reason strings, and on-device activity logs. See [permissions and consent](/guides/permissions/). ### Account and quota User-owned FKN sign-in via `connect()`, account metadata, and daily quota inspection. See [account and quota](/guides/account/). ### Packages Install and render third-party npm packages on isolated sandbox origins. See [packages](/guides/packages/). ### Publishing on fkn.dev Claim domains and npm packages, sign releases, ship store media on fkn.app, and configure payouts. See [publishing](/publishing/). --- # Overview URL: https://docs.fkn.dev/start/overview/ Source: src/content/docs/start/overview.md `@fkn/lib` extends a standard web app with system capabilities that browsers normally block: raw TCP and UDP sockets, cross-origin `fetch()` and `WebSocket`, end-to-end encrypted file and object storage, encrypted realtime rooms, live website framing, and sandboxed third-party packages. It runs on a web page, inside a dedicated Web Worker, and inside sandboxed packages. ## How the architecture fits together Your app stays a regular browser bundle. When it imports `@fkn/lib`, the library connects to FKN through the [broker frame](/reference/glossary/#broker-frame) or the [extension backend](/reference/glossary/#extension-backend), depending on what the user has installed and what each call requests. - **Cloud backend:** Works out of the box for every visitor with zero installs. Traffic travels through FKN relays (WebVPN) or the HTTP proxy, and files sync to the user's encrypted cloud storage. - **Extension backend:** When the visitor has the FKN browser extension installed, `fetch()` and `attachFrame()` can run straight from their own browser using their existing cookies and sessions, after explicit per-site consent. - **Worker and package runtimes:** Dedicated Web Workers can open sockets and fetch via `relayWorker()`. Third-party packages run on isolated sandbox origins under `sdbx.app` and talk to your app over typed message channels. See [backends](/guides/backends/) for exact routing rules and [glossary](/reference/glossary/) for key terms used across these docs. ## What people using your app keep Every app built on FKN shares one user-owned account model: - **Their files:** Files and objects encrypt inside the user's browser before upload. FKN servers hold ciphertext only and cannot open them. - **Their sign-in:** Users sign in on `fkn.app` with their password or passkey. Your app never sees their password, email address, or account key. - **Their permissions:** Users approve site interactions one origin at a time and can revoke any grant from the FKN bar or `fkn.app`. - **Free to use:** FKN is free for users and free to build on. Each user gets one daily allowance across every app they open and can choose Premium on `fkn.app` for higher limits. For exact plan allowances and privacy guarantees written for end users, see [plans and your allowance](https://www.fkn.app/help/plans/) and [your data](https://www.fkn.app/help/your-data/#what-fkns-servers-see). ## Where to go next - **First 5 minutes:** Follow the [quickstart](/start/quickstart/) to fetch a cross-origin URL, persist a file, and drive an attached frame, or read [install](/start/install/) for Vite and bundler setup. - **API guides:** Pick one surface under **Guides** in the sidebar, starting with [backends](/guides/backends/), [fetch()](/guides/fetch/), [TCP and UDP sockets](/guides/sockets/), [storage](/guides/storage/), [rooms](/guides/rooms/), or [frames](/guides/frames/). - **Task recipes:** Follow end-to-end recipes under [recipes](/recipes/) for worker sockets, C/WASM engines, chat rooms, sign-in flows, and production bundling. - **Shipping on fkn.dev:** Read [register and claim sources](/publishing/) and [signing, store media, and payouts](/publishing/signing/) to verify your app on `fkn.app`. - **Reference:** Look up [entry points](/reference/entry-points/), [limits and timeouts](/reference/limits/), [limitations](/reference/limitations/), [every error](/errors/), and the generated [API reference](/api/). - **Machine-readable docs:** Read [`/llms.txt`](https://docs.fkn.dev/llms.txt) for a concise page map, [`/llms-full.txt`](https://docs.fkn.dev/llms-full.txt) for all guides, recipes, publishing docs, and errors in one file, and [`/llms-api.txt`](https://docs.fkn.dev/llms-api.txt) for the full API reference. - **User help:** Looking for non-developer help? Visit the [FKN Help Guide](https://www.fkn.app/help/). --- # Quickstart URL: https://docs.fkn.dev/start/quickstart/ Source: src/content/docs/start/quickstart.mdx You can have a link inspector running on your own dev server in six steps: paste a URL, fetch it through the FKN cloud even though the site sends no CORS headers, show the title it found, keep the last results across reloads, then render the same page in a frame and read its heading. This page covers the install, the fetch and how to check it, the file that survives a reload, the attached frame, and a table of inputs that proves your handler tells a refusal from a rejection. Nothing on this path needs an account, an install or a consent prompt. A backend is where a call runs, and every call below runs on the cloud backend, FKN's own infrastructure, which the library reaches through the broker frame, a hidden `fkn.app` iframe it mounts on your page. How the root exports choose a backend is on [backends](/guides/backends/#how-the-root-exports-choose). Each step opens with a badge naming where its code runs, `[Build]` for the bundler configuration and `[Page]` for `app.ts`, the app's own file, as defined on [recipes](/recipes/). The page holds a text input, a list and an iframe, which the blocks refer to as `input`, `list` and `iframe`. The markup around them is yours. ## [Build] Install the library and the plugin In a [Vite](https://vite.dev/guide/) project, add the library and the plugin: ```sh npm install @fkn/lib npm install --save-dev @fkn/vite-plugin ``` `@fkn/lib` is the whole library in one package. The root import reaches `buffer`, `events` and `stream`, three Node modules a browser bundle has to supply, and `@fkn/vite-plugin` supplies them. It is one entry in your Vite plugins: ```ts title="vite.config.ts" import { defineConfig } from 'vite' import { fkn } from '@fkn/vite-plugin' export default defineConfig({ plugins: [fkn()], // buffer, events and stream are supplied, so the root import of @fkn/lib resolves }) ``` That entry is the whole build configuration. What else the plugin wires is on [`@fkn/vite-plugin`](/tooling/vite-plugin/#what-it-wires), and what a bundler other than Vite has to provide is on [install](/start/install/#what-your-bundler-must-supply). Load `app.ts` from your page as a module script. The library appends the broker frame to `document.body` when it evaluates, so the body has to exist by then, see [install](/start/install/#what-you-get). ## [Page] Fetch a URL the browser cannot `cloud.fetch` takes the same arguments as the platform's `fetch` and returns the same `Response`. The request leaves from the proxy, the FKN server that makes the request on your behalf, so the page's cross-origin rules do not apply to it and the site needs no CORS header. Fetch the pasted URL and read the title out of the HTML: ```ts title="app.ts" import { cloud } from '@fkn/lib' type Result = { url: string, title: string, status: number } const inspect = async (url: string): Promise => { const response = await cloud.fetch(url) // through the proxy, so the page's cross-origin rules do not apply response.ok // true when example.org answered 200 response.url // '', the Response was rebuilt on your side const html = await response.text() const title = new DOMParser().parseFromString(html, 'text/html').title // 'Example Domain' return { url, title, status: response.status } } const result = await inspect('https://example.org/') result.title // 'Example Domain', the title of example.org, parsed out of the body ``` `response.ok` and `response.status` are the site's own. `response.url` is `''` because the cloud rebuilds the `Response` on your side, and that empty string is how you can tell the proxy answered. Wire `inspect` to the input's `change` event and put each `result.title` in the list. The root `fetch` from `@fkn/lib` makes the same call and picks the extension, the FKN browser extension, when it is on the page. The quickstart pins `cloud.fetch` so the check in the next step holds on every page. How the root call decides is on [fetch()](/guides/fetch/#how-the-root-fetch-decides). ## [Page] Check it worked Paste `https://example.org/` and press Enter. Two things show the request went through the cloud: `Example Domain` appears in the list, and the Network panel of your browser's developer tools shows no request to `example.org`. The request that carried it went to the proxy, from the broker frame, and names the target in its `fkn-proxy-hostname` request header. A refusal from the proxy is not a rejection. The `Response` resolves with the proxy's own status and a JSON body naming the reason, so read `response.ok` before the body: ```ts title="app.ts" const response = await cloud.fetch('http://192.168.1.10/') // a private address, which the proxy refuses response.ok // false, and nothing threw response.status // 403, the proxy's own status const body: { error: string } = await response.json() body.error // 'egress refused (non-public target)' ``` The refusal arrived as an ordinary `Response`. A handler that shows `body.error` in the list when `response.ok` is false covers every answer the proxy gives on its own behalf. When the title does not appear, the failure is in one of five places. Test them in this order: 1. `response.ok` is `false`. A 403 with [`egress refused (non-public target)`](/errors/#egress-refused-non-public-target) means the host resolved to a non-public address or did not resolve at all. A 502 with [`upstream fetch failed: `](/errors/#upstream-fetch-failed) means the proxy reached the address and the site refused or stalled. A 429 with [`rate limit exceeded`](/errors/#rate-limit-exceeded) means this caller is past its fair-use limit for now. 2. The call rejected with [`fetch refuses FKN platform domains (fkn.app)`](/errors/#fetch-refuses-fkn-platform-domains). The pasted host is one of FKN's own, which every backend refuses before anything leaves the page, see [platform hosts](/guides/backends/#platform-hosts). 3. The call rejected with [`FKN cloud.fetch: no proxy is available`](/errors/#fkn-cloudfetch-no-proxy-is-available). The broker, the connection your page holds into FKN, could not read its list of proxies. Retry after [30,000 ms](/reference/limits/#endpoint-ttl-ms), when it reads the list again. 4. The call never settles. `cloud.fetch` waits for the broker with [no deadline](/reference/limits/#cloud-fetch-deadline), so a broker frame that never connects leaves it pending. Look for the hidden `fkn.app` iframe in `document.body`. The library appends it when it evaluates, and the body has to exist by then, which a module script guarantees. When the frame is there and the page is served with cross-origin isolation headers, see [install](/start/install/#optional-serve-a-cross-origin-isolated-page). 5. `response.ok` is `true` and the title is empty. The URL answered something other than an HTML document with a `title`, so there was nothing to parse. Every other message has a row on [every error](/errors/#fetch). ## [Page] Keep the results across reloads `fs` is the default file system of `@fkn/lib`, a path-based subset of Node's `fs` that keeps every file on this device and, when an account is connected, a copy in the account. The account is the FKN identity a person carries between sites, and nothing here connects one, so the list stays on this device. The three file systems are on [storage](/guides/storage/#three-file-systems). Write the list after every inspect: ```ts title="app.ts" await fs.mount() // fills the in-memory layer from this device, once per page load const save = async (results: Result[]) => { await fs.promises.writeFile('library/links.json', JSON.stringify(results)) // in memory now, on this device 250 ms later await fs.flush() // the write to this device runs now rather than on the timer } ``` `mount()` fills the in-memory layer, the copy of every file that the synchronous calls read. The promise forms await it on their own, so calling it first only moves that wait to page load. A write goes to memory and reaches this device's storage [250 ms](/reference/limits/#flush-debounce) later, or when `flush()` runs, and a reload after either still lists it. Read it back when the page loads, and treat a missing file as an empty list: ```ts title="app.ts" const load = async (): Promise => { try { return JSON.parse(String(await fs.promises.readFile('library/links.json', 'utf8'))) // the results from the last visit } catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return [] // nothing saved yet, so the list starts empty throw error } } const results = await load() // [] on the first visit, and the saved list after a reload ``` The read is wrapped in `String(...)` because the return type is `Buffer | string` whatever encoding you pass. A missing file rejects with an `Error` whose `code` is `ENOENT`, the one code that means nothing is there, so the first visit starts empty rather than failing. Every other code is rethrown, see [error codes](/guides/storage/#error-codes). The account's own file system, `cloud.fs`, reports absence as a `StorageNotFoundError` that `isNotFound()` recognises, described on the same page. ## [Page] Render the page and read it `attachFrame()` puts another site inside your iframe and hands back a `Frame` you navigate and drive, an attached frame. The root call takes the render proxy, the cloud frame backend, on its default `cookies: 'persistent'`, whatever is installed and with no wait for the extension. The iframe has to be in the document already, with no `sandbox` attribute or one that includes `allow-scripts` and `allow-same-origin`: ```ts title="app.ts" const frame = await attachFrame({ iframe }) // the render proxy took the iframe, with no install card on the way await frame.goto('https://example.org/') // resolves once example.org has loaded inside your iframe ``` The site renders inside your own iframe, under an identity bar the cloud draws naming your app. `goto()` resolves on the frame's load event, and every wait on the way to a working frame is bounded, see [timeouts](/guides/frames/#timeouts). Read the heading out of it: ```ts title="app.ts" const heading = await frame.locator('h1').textContent() // 'Example Domain', read out of the framed page with no prompt ``` `locator('h1')` starts a locator chain, a `Locator` built by chaining selectors, and nothing touches the page until `textContent()` runs. The action needs exactly one match. It is retried for [30,000 ms](/reference/limits/#locator-timeout) and then rejects with [`No elements found`](/errors/#no-elements-found) on a page with no `h1`, or with [`Strict mode violation: locator resolved to 2 elements`](/errors/#strict-mode-violation-locator-resolved-to) on a page with two. Every selector and action is on [locators and actions](/guides/locators/), and every option `attachFrame()` takes is on [frames](/guides/frames/#options). **NOTE:** With `cookies: 'native'` the same `attachFrame()` lands on the extension backend, on the person's own browser cookies, and the read asks the user once on the consent sheet, the prompt the extension shows before an action that needs their approval. On the cloud backend no action other than `fetch()` asks anything. The demo below is this step on a real site. It attaches a blank iframe, navigates it to the Wikipedia search page and restyles it, then reads the heading, fills the search box and presses the search button, on the render proxy when no extension is on the page: [Interactive demo: Acting on a real site, guided](https://docs.fkn.dev/demos/attach-frame/guided/) The same call you wrote read the heading there. Open it in a new tab and the whole run happens on a page of its own. ## Try it out Four inputs to paste, with the row that makes the other three mean something first: | What you put in | What you get | How to check | |---|---|---| | A public URL that answers, `https://example.org/` | The title, and a `Response` with `ok` true | The control: when this row fails, nothing below it means anything | | A hostname that does not resolve, or a private address such as `http://192.168.1.10/` | A resolved `Response`, status 403, body [`{"error":"egress refused (non-public target)"}`](/errors/#egress-refused-non-public-target) | Read `response.ok` and the body. Nothing throws | | A public host that refuses the connection or gives no answer within [10,000 ms](/reference/limits/#connect-timeout) | A resolved `Response`, status 502, body [`{"error":"upstream fetch failed: "}`](/errors/#upstream-fetch-failed) | The same shape, and this one is worth a retry | | `https://fkn.app/` | A thrown `Error` with the message [`fetch refuses FKN platform domains (fkn.app)`](/errors/#fetch-refuses-fkn-platform-domains) | This one rejects, which is the difference the table is teaching | The last row is the one refusal on this path that throws, because the library refuses the target itself before anything reaches the proxy: ```ts title="app.ts" try { await cloud.fetch('https://fkn.app/') } catch (error) { (error as Error).message // 'fetch refuses FKN platform domains (fkn.app)', thrown before anything left the page } ``` Nothing left the page, so there is no `Response` to read. A handler that shows the title for the first row, `body.error` for the second and third, and the thrown message for the fourth tells the three shapes apart rather than merely not crashing. ## Optional: Use the user's own browser session With the extension on the page, a request can leave from the user's own browser and carry their session, with the cloud as the fallback when it is not there, see [use the extension or the cloud per request](/recipes/extension-or-cloud/). ## Optional: Connect an account `connect()` asks the person to use their FKN account on this site, which gives `fs` a second copy of every file in the account and `cloud.quota()` a metered volume to report, see [account and quota](/guides/account/). ## Optional: Open a TCP socket `@fkn/lib/net` and `@fkn/lib/dgram` are Node's `net` and `dgram` in the browser, with the bytes carried through the relay, the server that holds the real socket at the far end, see [TCP and UDP sockets](/guides/sockets/). ## Optional: Move the work to a worker A worker holds no broker of its own, so the page relays it with `await relayWorker(worker, { unregisterSignal })` and the engine inside opens sockets exactly as a page would, see [run sockets in a worker](/recipes/sockets-in-a-worker/). ## Optional: Add a package A package is an npm module FKN loads on a sandbox origin of its own, and your app installs one, connects to it and draws it inside its own layout, see [install a package and show its UI](/recipes/use-a-package/). ## Optional: Ship it A production build supplies the same three shims, and a page served with cross-origin isolation headers needs two more lines in the config, see [install](/start/install/). ## See also - [install](/start/install/) - [fetch()](/guides/fetch/) - [storage](/guides/storage/) - [frames](/guides/frames/) - [locators and actions](/guides/locators/) - [every error](/errors/) --- # Install URL: https://docs.fkn.dev/start/install/ Source: src/content/docs/start/install.md `@fkn/lib` is one npm package, and a web app adds it with one install. A bundle that carries the socket surface also needs three Node shims. This page covers what the package ships, which import path to take, the three shims your bundler has to supply, the optional React peer, the one runtime requirement, and the headers for a cross-origin isolated page. ## Install the package Every command on this site uses npm, and any package manager that reads `package.json` works the same way: ```sh npm install @fkn/lib ``` That one line is the whole install. The package is ESM first, and every import path also carries a `require` condition for a CommonJS build. ## What you get Every import path ships three files: an ESM `.js`, a CJS `.cjs` and one bundled `.d.ts`. There are 31 of them, plus the manifest at `@fkn/lib/package.json` for tooling that reads the version. [entry points](/reference/entry-points/) lists every one and what it gives you. `osra`, `ip-address` and `@types/node` arrive as dependencies. `@types/node` is a runtime dependency rather than a dev one, because the public `net`, `dgram`, `http` and `fs` declarations reference Node's builtin types. The broker is the connection your app holds into FKN, and every entry that reaches it mounts a hidden `fkn.app` iframe, the broker frame, or adopts one the page already holds, as soon as it evaluates in a window. The library appends a new frame to `document.body`, so the body has to exist by then. The exact conditions are on [how it works](/internals/how-it-works/#the-broker-frame). **NOTE:** The manifest declares no `sideEffects` field, so a bundler keeps every import you write, which is right for an import that mounts a frame. ## Pick a subpath or the root The root entry gathers most of the library under one import, and each subpath carries one piece of it. The two spellings are one implementation: `cloud.fetch` from the root and `fetch` from `@fkn/lib/cloud/fetch` are the same function. Six subpaths stay separate, because the root does not re-export them: `@fkn/lib/api`, `@fkn/lib/react`, `@fkn/lib/contract`, `@fkn/lib/wire`, `@fkn/lib/messages` and `@fkn/lib/attach-policy`, all listed on [entry points](/reference/entry-points/). What differs is what the published files pull in. `@fkn/lib/cloud/fetch` reaches only `osra`. The root also pulls `buffer`, `events` and `stream`, three Node builtins a browser bundle has to shim, because the socket surface is built on Node's stream and event classes, and the file and datagram surfaces hand back a `Buffer`. An app that fetches and nothing more can take the narrow path and skip the shims: ```ts title="app.ts" import { fetch } from '@fkn/lib/cloud/fetch' const response = await fetch('https://example.org/api/catalog.json') // a Response through the proxy, and no stream shim behind the import const catalog = await response.json() // the app's own data ``` The request went through the proxy, the server that `cloud.fetch` sends a request through, and no socket code came along. What the proxy answers is on [fetch()](/guides/fetch/#cloudfetch). The narrow path carries less code, and it still mounts the broker frame exactly as the root does. Take the root when the app uses several surfaces, since the shims below cover all of it. ## What your bundler must supply The published files import `buffer`, `events` and `stream` as bare specifiers, only where a shape uses them: | Entry | What it reaches | |---|---| | `@fkn/lib`, `@fkn/lib/cloud`, `@fkn/lib/cloud/http`, `@fkn/lib/http` | `buffer`, `events`, `stream`, `ip-address`, `osra` | | `@fkn/lib/net`, `@fkn/lib/cloud/net` | `events`, `stream`, `ip-address`, `osra` | | `@fkn/lib/dgram`, `@fkn/lib/cloud/dgram` | `buffer`, `events`, `ip-address`, `osra` | | `@fkn/lib/fs`, `@fkn/lib/fs/promises`, `@fkn/lib/cloud/fs`, `@fkn/lib/cloud/fs/promises` | `buffer`, `osra` | | `@fkn/lib/opfs`, `@fkn/lib/opfs/promises` | `buffer` | | `@fkn/lib/cloud/fetch`, `@fkn/lib/dns`, `@fkn/lib/cloud/dns`, `@fkn/lib/account`, `@fkn/lib/packages`, `@fkn/lib/extension`, `@fkn/lib/api` | `osra` | | `@fkn/lib/react` | `react` | | `@fkn/lib/desktop`, `@fkn/lib/contract`, `@fkn/lib/wire`, `@fkn/lib/messages`, `@fkn/lib/attach-policy` | none | `osra`, `ip-address` and `react` are packages your bundler resolves on its own, and the three that remain are the shims. So `net` needs `events` and `stream`, `dgram` needs `buffer` and `events`, `http` needs all three, and `fs` needs `buffer`. A browser has none of them, so the bundle has to provide each name before the entry that imports it evaluates. The stream shim also reads `global` at module scope, so the globals matter as much as the modules. With Vite, `vite-plugin-node-polyfills` scoped to exactly those three does both, and it defines `global`, `process` and `Buffer` by default. Add it as a dev dependency and list the three names: ```ts title="vite.config.ts" import { defineConfig } from 'vite' import { nodePolyfills } from 'vite-plugin-node-polyfills' export default defineConfig({ plugins: [ nodePolyfills({ include: ['buffer', 'events', 'stream'], protocolImports: false }), // a build in which @fkn/lib/net resolves its bare stream import ], }) ``` That is the configuration this site builds with: three names and nothing else. `protocolImports: false` is enough for the library, whose published files import the three names bare. Set it to `true` when your own code imports `node:buffer`, `node:events` or `node:stream`. [`@fkn/vite-plugin`](/tooling/vite-plugin/#what-it-wires) supplies the same shims and aliases the Node names to the matching entries, so code written for Node keeps its imports. With another bundler, alias the same three names to browser implementations and define the same three globals. The production build is on [bundle an app that uses sockets](/recipes/bundling/). ## React `react` is an optional peer at `>=18`, and only `@fkn/lib/react` imports it. Installing `@fkn/lib` does not pull React in. Install React only when you render `ConnectButton`, the one component the package exports, which is described under [account and quota](/guides/account/#the-connect-button). ## The one runtime requirement A realm is one JavaScript execution context, such as a window or a worker. `@fkn/lib` needs one of two: a window realm, where it mounts the broker frame and talks to the broker through it, or a worker the page relayed. A worker is a realm of its own, and it cannot open a broker connection by itself, so the page relays its calls. Inside a worker there is no document to mount the frame in, so the worker reaches the broker only once the page awaits `relayWorker`. What the relay does, its options, and how it ends are on [workers](/guides/workers/#what-the-relay-does). After that the worker imports `@fkn/lib/net` exactly as the page does. See [run sockets in a worker](/recipes/sockets-in-a-worker/) for the pair, and [workers](/guides/workers/#what-works-in-a-relayed-worker) for what works inside one. A test runner has no broker to reach, and what runs without one is on [testing](/tooling/testing/). ## Optional: Pre-mount the broker frame `@fkn/lib` mounts the broker frame when your bundle runs, or you can inject `brokerFrameScript` from `@fkn/lib/broker-frame` at the top of `` so the frame loads while the rest of your JavaScript downloads: ```ts title="vite.config.ts" import { defineConfig } from 'vite' import { brokerFrameScript } from '@fkn/lib/broker-frame' export default defineConfig({ plugins: [ { name: 'fkn-broker-frame', transformIndexHtml: () => [{ tag: 'script', children: brokerFrameScript, injectTo: 'body-prepend' }], }, ], }) ``` In a framed page or inside `fkn.app`, the script mounts nothing and the library reuses the surrounding broker. Use `brokerFrameScript` rather than writing a raw `