@kerf-js/realtime-livekit 0.0.0-stage → 0.1.1-nightly.20261112.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ravonus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ persons to whom the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,255 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <a href="https://github.com/Ravonus/kerf">
3
+ <img src="https://raw.githubusercontent.com/Ravonus/kerf/main/site/public/logo.svg" width="88" height="88" alt="Kerf">
4
+ </a>
5
+ </p>
2
6
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
7
+ <h1 align="center">@kerf-js/realtime-livekit</h1>
8
+
9
+ <p align="center">
10
+ <strong>Live cursors over a LiveKit room, with the token signed by your Kerf server.</strong><br>
11
+ LiveKit proves who sent each frame, and the same room can carry audio and video.
12
+ </p>
13
+
14
+ <p align="center">
15
+ <a href="https://www.npmjs.com/package/@kerf-js/realtime-livekit"><img src="https://img.shields.io/npm/v/@kerf-js/realtime-livekit?color=145c3c&label=npm" alt="npm version"></a>
16
+ <a href="https://github.com/Ravonus/kerf/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-145c3c" alt="MIT license"></a>
17
+ <a href="https://github.com/Ravonus/kerf/actions"><img src="https://img.shields.io/github/actions/workflow/status/Ravonus/kerf/quality.yml?branch=main&label=checks" alt="checks"></a>
18
+ </p>
19
+
20
+ <p align="center">
21
+ <a href="#quick-start">Quick start</a> ·
22
+ <a href="https://github.com/Ravonus/kerf/tree/main/docs">Docs</a> ·
23
+ <a href="https://github.com/Ravonus/kerf/tree/main/examples">Examples</a> ·
24
+ <a href="https://github.com/Ravonus/kerf/blob/main/CHANGELOG.md">Changelog</a> ·
25
+ <a href="https://github.com/Ravonus/kerf">GitHub</a>
26
+ </p>
27
+
28
+ > [!IMPORTANT]
29
+ > **Early release (0.1.x).** APIs can change between minor versions. Pin the version and read the
30
+ > [changelog](https://github.com/Ravonus/kerf/blob/main/CHANGELOG.md) before you upgrade.
31
+
32
+ A cursor transport for [`@kerf-js/multiplayer`](https://www.npmjs.com/package/@kerf-js/multiplayer) <sup>[source](https://github.com/Ravonus/kerf/tree/main/packages/multiplayer)</sup>. By
33
+ default cursors go through your server over the channel's socket. With this package they go through a
34
+ [LiveKit](https://livekit.io) room instead, which takes that traffic off your server. It suits an app that already uses
35
+ LiveKit, or wants audio and video in the same room as its cursors.
36
+
37
+ ## Highlights
38
+
39
+ - **Your server signs the token.** `livekitTicket()` signs a LiveKit access token whose identity is the channel
40
+ connection's id. No client can name itself to LiveKit.
41
+ - **The sender is proven.** A frame's sender is the LiveKit participant's identity, and `@kerf-js/multiplayer` only
42
+ accepts frames from connections that are in the room.
43
+ - **Data only by default.** The token allows joining, subscribing and publishing data. `media: true` adds publishing
44
+ audio and video.
45
+ - **Rooms that say nothing.** The LiveKit room name is a hash of the channel's params, so LiveKit never sees them.
46
+ - **Falls back on its own.** Until every peer is in the LiveKit room, cursors keep going over the socket.
47
+ - **Lossy on purpose.** Cursor frames are published with `reliable: false` on the topic `kerf`: a late cursor is
48
+ replaced by the next one.
49
+
50
+ ## Install
51
+
52
+ ```bash
53
+ # npm
54
+ npm install @kerf-js/multiplayer @kerf-js/realtime-livekit
55
+
56
+ # pnpm
57
+ pnpm add @kerf-js/multiplayer @kerf-js/realtime-livekit
58
+
59
+ # bun
60
+ bun add @kerf-js/multiplayer @kerf-js/realtime-livekit
61
+ ```
62
+
63
+ Or install `@kerf-js/multiplayer`, then let the `kerf` CLI install this package and add its plugin to
64
+ `kerf.config.ts`:
65
+
66
+ ```bash
67
+ npm install @kerf-js/multiplayer
68
+ npx kerf add realtime-livekit
69
+ ```
70
+
71
+ `@kerf-js/multiplayer` is a peer dependency, so add it too. `livekit-client` comes with this package.
72
+
73
+ `kerf add` installs the package and puts its plugin in `kerf.config.ts`:
74
+
75
+ ```ts
76
+ // kerf.config.ts
77
+ import { defineConfig } from '@kerf-js/core/config'
78
+ import realtimeLivekit from '@kerf-js/realtime-livekit/plugin'
79
+
80
+ export default defineConfig({
81
+ plugins: [realtimeLivekit()],
82
+ })
83
+ ```
84
+
85
+ The plugin fills no slot. It records what the app reaches (LiveKit from the browser, and the `LIVEKIT_API_KEY` and
86
+ `LIVEKIT_API_SECRET` variables), and tells agents how the transport is used. The transport itself is passed to
87
+ `presence()` in a page.
88
+
89
+ ## Setup
90
+
91
+ You need a LiveKit server: [LiveKit Cloud](https://livekit.io) or your own. The server reads two variables:
92
+
93
+ | Variable | Where | What it is |
94
+ | -------------------- | ----------- | ---------- |
95
+ | `LIVEKIT_API_KEY` | your server | The key `livekitTicket()` puts in the token's `iss`. |
96
+ | `LIVEKIT_API_SECRET` | your server | The secret it signs the token with (HS256). Never send it to a page. |
97
+
98
+ The page needs the LiveKit URL (`wss://…`). It isn't secret: read it on the server and hand it to the page, for
99
+ example from a route's `load`.
100
+
101
+ ## Quick start
102
+
103
+ Give `multiplayer()` the ticket:
104
+
105
+ ```ts
106
+ // src/server/doc.ts
107
+ import { channel } from '@kerf-js/start'
108
+ import { multiplayer } from '@kerf-js/multiplayer/server'
109
+ import { livekitTicket } from '@kerf-js/realtime-livekit/server'
110
+ import { z } from 'zod'
111
+
112
+ const mp = multiplayer<{ id: string }>({
113
+ user: ({ ctx }) => ({ id: ctx.user!.id, name: ctx.user!.name }),
114
+ ticket: livekitTicket(), // LIVEKIT_API_KEY and LIVEKIT_API_SECRET
115
+ })
116
+
117
+ export const doc = channel({
118
+ params: z.object({ id: z.string().max(64) }),
119
+ authorize: ({ ctx }) => !!ctx.user,
120
+ client: { ...mp.client },
121
+ server: { ...mp.server },
122
+ on: { ...mp.on },
123
+ left: mp.left,
124
+ })
125
+ ```
126
+
127
+ Pass the transport to `presence()`:
128
+
129
+ ```tsx
130
+ // src/routes/room.tsx
131
+ import { cursors, presence } from '@kerf-js/multiplayer'
132
+ import { Cursors } from '@kerf-js/multiplayer/ui'
133
+ import { livekit } from '@kerf-js/realtime-livekit'
134
+ import { doc } from '#/server/doc'
135
+
136
+ export const load = () => ({ livekit: process.env.LIVEKIT_URL ?? '' })
137
+
138
+ export default function Room(props: { data: { livekit: string } }) {
139
+ const chat = doc.connect({ id: 'lobby' })
140
+ const live = presence(chat, {
141
+ transport: livekit({ url: props.data.livekit }),
142
+ })
143
+ const pointers = cursors(live)
144
+ return (
145
+ <main>
146
+ <p>Cursors via {live.via.value}</p>
147
+ <Cursors of={pointers} />
148
+ </main>
149
+ )
150
+ }
151
+ ```
152
+
153
+ `live.via.value` is `'socket'` until every other peer is in the LiveKit room, then `'livekit'`.
154
+
155
+ ## How it works
156
+
157
+ 1. A page joins the channel and sends `mp.hello`. The server calls `livekitTicket()` with the connection's id, the
158
+ peer's `user` and the channel's params, and returns the token with `mp.you`.
159
+ 2. `livekit({ url })` connects a `livekit-client` `Room` to `url` with that token. Without a ticket it doesn't
160
+ connect, and cursors stay on the socket.
161
+ 3. A cursor is published as data on the topic `kerf`, unreliable. A received frame is handed to `presence` with the
162
+ participant's identity as the sender, and checked there like any frame off a transport.
163
+ 4. The link is `up` while every peer the channel knows is also a remote participant in the LiveKit room.
164
+
165
+ ## API reference
166
+
167
+ ### `@kerf-js/realtime-livekit`
168
+
169
+ `livekit(options): PeerTransport`: the transport, for `presence(conn, { transport })`. Its `open` returns the
170
+ `PeerLink` with one more field, `room`: the `livekit-client` `Room`, for audio and video.
171
+
172
+ | Option | Type | Default | What it does |
173
+ | ------ | -------- | -------- | ------------ |
174
+ | `url` | `string` | required | The LiveKit server, `wss://…`. |
175
+
176
+ Type: `LivekitOptions`.
177
+
178
+ ### `@kerf-js/realtime-livekit/server`
179
+
180
+ `livekitTicket(options?)`: returns the function `multiplayer({ ticket })` calls. It refuses to run without a key and
181
+ secret.
182
+
183
+ | Option | Type | Default | What it does |
184
+ | -------- | --------- | ----------------------------- | ------------ |
185
+ | `key` | `string` | `process.env.LIVEKIT_API_KEY` | The API key, the token's `iss`. |
186
+ | `secret` | `string` | `process.env.LIVEKIT_API_SECRET` | The API secret the token is signed with. |
187
+ | `ttl` | `number` | `3600` | Seconds the token is valid. |
188
+ | `media` | `boolean` | `false` | Also allow publishing audio and video (`canPublish`). Data only is what cursors need. |
189
+
190
+ The token's claims: `iss` (the key), `sub` (the connection id), `name` (`user.name`), `nbf`, `exp`, and a `video`
191
+ grant with `room` (`roomId(params)` from `@kerf-js/multiplayer/server`), `roomJoin`, `canPublish` (from `media`),
192
+ `canSubscribe` and `canPublishData`. Type: `LivekitTicketOptions`.
193
+
194
+ ### `@kerf-js/realtime-livekit/plugin`
195
+
196
+ The plugin for `kerf.config.ts`, as a default export and as the named export `livekit`. Import the default, or rename
197
+ the named one, so it doesn't clash with the transport of the same name.
198
+
199
+ ## Recipes
200
+
201
+ **Audio and video in the same room.** Sign tokens that may publish media:
202
+
203
+ ```ts
204
+ const mp = multiplayer({
205
+ user,
206
+ ticket: livekitTicket({ media: true }),
207
+ })
208
+ ```
209
+
210
+ The `room` on the link `livekit()` opens is a `livekit-client` `Room`, so its own API publishes tracks.
211
+
212
+ **Shorter-lived tokens.**
213
+
214
+ ```ts
215
+ livekitTicket({ ttl: 600 })
216
+ ```
217
+
218
+ ## Limits
219
+
220
+ - Only cursors ride LiveKit. Presence state, the relay and signalling stay on the channel, where the server checks them.
221
+ - Frames are unreliable. A lost cursor is replaced by the next; nothing is retried.
222
+ - A connection gets its token once, with `mp.you`. A token past `ttl` can't open a new LiveKit connection; a new
223
+ `mp.hello` (a reconnect or a reset) brings a fresh one.
224
+
225
+ ## Troubleshooting
226
+
227
+ | Symptom | Cause | Fix |
228
+ | ------- | ----- | --- |
229
+ | `` `livekitTicket` needs `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET` `` on join | The server has neither variable nor options. | Set both variables, or pass `key` and `secret`. |
230
+ | `via` stays `'socket'` | No ticket reached the page (no `ticket` in `multiplayer()`), the URL is wrong, LiveKit refused the token, or a peer hasn't joined LiveKit yet. Connection errors are swallowed. | Check the URL and the key pair; cursors keep working over the socket meanwhile. |
231
+ | `kerf check` stops after `kerf add` | A newly added plugin's permissions must be approved. | `kerf plugins approve @kerf-js/realtime-livekit`. |
232
+
233
+ ## Related packages
234
+
235
+ | Package | What it is |
236
+ | ------- | ---------- |
237
+ | [`@kerf-js/multiplayer`](https://www.npmjs.com/package/@kerf-js/multiplayer) | Presence, cursors and the relay that this transport carries cursors for. |
238
+ | [`@kerf-js/realtime-webrtc`](https://www.npmjs.com/package/@kerf-js/realtime-webrtc) | Cursors browser to browser, with no service in between. |
239
+ | [`@kerf-js/realtime-socketio`](https://www.npmjs.com/package/@kerf-js/realtime-socketio) | Cursors through a Socket.IO relay you run. |
240
+ | [`@kerf-js/realtime-redis`](https://www.npmjs.com/package/@kerf-js/realtime-redis) | A `realtime.bus` over Redis: how server instances reach each other, not a cursor transport. |
241
+ | [`@kerf-js/realtime-nats`](https://www.npmjs.com/package/@kerf-js/realtime-nats) | A `realtime.bus` over NATS. |
242
+ | [`@kerf-js/realtime-durable-object`](https://www.npmjs.com/package/@kerf-js/realtime-durable-object) | A `realtime.bus` on Cloudflare Durable Objects. |
243
+ | [`@kerf-js/core`](https://www.npmjs.com/package/@kerf-js/core) | The compiler and reactive runtime, the Vite plugin, the plugin system and test utilities. |
244
+ | [`@kerf-js/start`](https://www.npmjs.com/package/@kerf-js/start) | The full-stack runtime: routes, server functions, `channel`, `realtime()` and the realtime hub. |
245
+
246
+ ## Contributing
247
+
248
+ See [CONTRIBUTING.md](https://github.com/Ravonus/kerf/blob/main/CONTRIBUTING.md). From the repository,
249
+ `pnpm --filter @kerf-js/realtime-livekit test` checks the token against LiveKit's rules. The transport against a real
250
+ LiveKit server runs in Chromium through `@kerf-js/multiplayer`'s end-to-end test with `--via livekit`, which needs
251
+ `LIVEKIT_URL`, `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET`.
252
+
253
+ ## License
254
+
255
+ [MIT](https://github.com/Ravonus/kerf/blob/main/LICENSE)
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "@kerf-js/realtime-livekit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1-nightly.20261112.42",
4
+ "description": "@kerf-js/realtime-livekit: cursors over a LiveKit room, with the token signed by the room",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Ravonus/kerf.git",
9
+ "directory": "packages/realtime-livekit"
10
+ },
11
+ "homepage": "https://github.com/Ravonus/kerf/tree/main/packages/realtime-livekit#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/Ravonus/kerf/issues"
14
+ },
15
+ "type": "module",
16
+ "exports": {
17
+ ".": "./src/index.ts",
18
+ "./server": "./src/server.ts",
19
+ "./plugin": "./plugin.mjs"
20
+ },
21
+ "dependencies": {
22
+ "livekit-client": "2.23.0"
23
+ },
24
+ "peerDependencies": {
25
+ "@kerf-js/start": "^0.1.1-nightly.20261112.42",
26
+ "@kerf-js/core": "^0.1.1-nightly.20261112.42",
27
+ "@kerf-js/multiplayer": "^0.1.1-nightly.20261112.42"
28
+ },
29
+ "engines": {
30
+ "node": ">=24"
31
+ },
32
+ "keywords": [
33
+ "kerf"
34
+ ],
35
+ "files": [
36
+ "src",
37
+ "plugin.mjs",
38
+ "plugin.d.mts",
39
+ "README.md",
40
+ "LICENSE"
41
+ ],
42
+ "publishConfig": {
43
+ "access": "public"
44
+ }
6
45
  }
package/plugin.d.mts ADDED
@@ -0,0 +1,5 @@
1
+ import type { PluginInstance } from '@kerf-js/core/plugin'
2
+
3
+ declare const plugin: (options?: Record<string, unknown>) => PluginInstance<unknown>
4
+ /** `livekit()` for `kerf.config.ts`: puts LiveKit on the record of what the app reaches. A page passes the transport to `presence`. */
5
+ export default plugin
package/plugin.mjs ADDED
@@ -0,0 +1,17 @@
1
+ // livekit() for kerf.config.ts: no slot, since a page passes the transport to `presence`.
2
+ import { definePlugin } from '@kerf-js/core/plugin'
3
+
4
+ export const livekit = definePlugin({
5
+ name: '@kerf-js/realtime-livekit',
6
+ version: '0.1.0',
7
+ kerf: '*',
8
+ permissions: { net: ['browser:livekit'], env: ['LIVEKIT_API_KEY', 'LIVEKIT_API_SECRET'] },
9
+ contribute: () => ({
10
+ agents: {
11
+ title: 'Peer transport (LiveKit)',
12
+ text: '`presence(conn, { transport: livekit({ url }) })` sends cursors through LiveKit, and `link.room` is the room for audio and video. Pass `ticket: livekitTicket({ media })` from `@kerf-js/realtime-livekit/server` to `multiplayer()`, with `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET` on the server.',
13
+ },
14
+ }),
15
+ })
16
+
17
+ export default livekit
package/src/index.ts ADDED
@@ -0,0 +1,61 @@
1
+ // Cursors over a LiveKit room. `ticket` from the server is the token, and LiveKit proves a frame's sender: a participant's
2
+ // identity is the connection the room signed it for. The room is there for audio and video too: `link.room`.
3
+
4
+ import { createRoot, derived, state, untrack } from '@kerf-js/core'
5
+ import { Room, RoomEvent } from 'livekit-client'
6
+ import type { PeerLink, PeerRoom, PeerTransport } from '@kerf-js/multiplayer'
7
+
8
+ const TOPIC = 'kerf'
9
+
10
+ /** Options of `livekit()`. */
11
+ export interface LivekitOptions {
12
+ /** `wss://…` */
13
+ url: string
14
+ }
15
+
16
+ /** A peer transport over a LiveKit room, for `presence(conn, { transport })`. The link's `room` carries audio and video too. */
17
+ export function livekit({ url }: LivekitOptions): PeerTransport {
18
+ return {
19
+ name: 'livekit',
20
+ open(room: PeerRoom): PeerLink & { room: Room } {
21
+ return createRoot((dispose) => {
22
+ const live = new Room()
23
+ const here = state<string[]>([])
24
+ const sync = () => {
25
+ here.value = [...live.remoteParticipants.values()].map((p) => p.identity)
26
+ }
27
+ const decoder = new TextDecoder(),
28
+ encoder = new TextEncoder()
29
+ live.on(RoomEvent.DataReceived, (bytes, participant, _kind, topic) => {
30
+ if (topic === TOPIC && participant)
31
+ room.receive(participant.identity, decoder.decode(bytes))
32
+ })
33
+ for (const e of [
34
+ RoomEvent.ParticipantConnected,
35
+ RoomEvent.ParticipantDisconnected,
36
+ RoomEvent.Connected,
37
+ RoomEvent.Disconnected,
38
+ ])
39
+ live.on(e, sync)
40
+ if (room.ticket) void live.connect(url, room.ticket as string).then(sync, () => {})
41
+ return {
42
+ room: live,
43
+ up: derived(() => {
44
+ const ids = room.peers.value
45
+ return ids.length > 0 && ids.every((id) => here.value.includes(id))
46
+ }),
47
+ send(text) {
48
+ if (untrack(() => here.value.length))
49
+ void live.localParticipant
50
+ .publishData(encoder.encode(text), { reliable: false, topic: TOPIC })
51
+ .catch(() => {})
52
+ },
53
+ close() {
54
+ void live.disconnect()
55
+ dispose()
56
+ },
57
+ }
58
+ })
59
+ },
60
+ }
61
+ }
package/src/server.ts ADDED
@@ -0,0 +1,45 @@
1
+ // The token, for `multiplayer({ ticket })`: signed with the room's identity, so no client can name itself to LiveKit.
2
+ import { roomId, signTicket } from '@kerf-js/multiplayer/server'
3
+
4
+ /** Options of `livekitTicket()`. */
5
+ export interface LivekitTicketOptions {
6
+ /** Default `LIVEKIT_API_KEY`. */
7
+ key?: string
8
+ /** Default `LIVEKIT_API_SECRET`. */
9
+ secret?: string
10
+ /** Seconds. Default 3600. */
11
+ ttl?: number
12
+ /** Audio and video too. Default false: data only, which is what cursors need. */
13
+ media?: boolean
14
+ }
15
+
16
+ /** For `multiplayer({ ticket })`: a LiveKit token that names the connection and its room. Refuses to run without a key and secret. */
17
+ export const livekitTicket =
18
+ ({
19
+ key = process.env.LIVEKIT_API_KEY,
20
+ secret = process.env.LIVEKIT_API_SECRET,
21
+ ttl = 3600,
22
+ media = false,
23
+ }: LivekitTicketOptions = {}) =>
24
+ async ({ id, user, params }: { id: string; user: { name?: string }; params: unknown }) => {
25
+ if (!key || !secret)
26
+ throw new Error('`livekitTicket` needs `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET`')
27
+ const now = Math.floor(Date.now() / 1000)
28
+ return signTicket(
29
+ {
30
+ iss: key,
31
+ sub: id,
32
+ name: user.name,
33
+ nbf: now,
34
+ exp: now + ttl,
35
+ video: {
36
+ room: await roomId(params),
37
+ roomJoin: true,
38
+ canPublish: media,
39
+ canSubscribe: true,
40
+ canPublishData: true,
41
+ },
42
+ },
43
+ secret,
44
+ )
45
+ }