-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllms.txt
More file actions
135 lines (102 loc) · 4.6 KB
/
llms.txt
File metadata and controls
135 lines (102 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# @fluxstack/live
> Real-time server-client state synchronization framework for TypeScript.
> Server components with reactive state, actions, rooms, auth, file uploads, and binary deltas.
## What is FluxStack Live?
FluxStack Live lets you write **server-side components** that automatically synchronize state to connected browser clients over WebSocket. Think Phoenix LiveView for the TypeScript/Node.js ecosystem, but framework-agnostic on both server (Elysia, Express, Fastify) and client (React, vanilla JS, Vue).
## Packages
| Package | npm | Purpose |
|---|---|---|
| `@fluxstack/live` | Core | Server framework: LiveComponent, LiveServer, rooms, auth, security |
| `@fluxstack/live-elysia` | Adapter | Elysia.js transport adapter |
| `@fluxstack/live-express` | Adapter | Express + ws transport adapter |
| `@fluxstack/live-fastify` | Adapter | Fastify + @fastify/websocket transport adapter |
| `@fluxstack/live-client` | Client | Framework-agnostic browser WebSocket client |
| `@fluxstack/live-react` | React | React hooks and provider (`Live.use()`, `useLiveComponent()`) |
| `@fluxstack/live-redis` | Adapter | Redis pub/sub adapter for horizontal scaling |
## Quick Start (Elysia + React)
### Server (server/live/Counter.ts)
```typescript
import { LiveComponent } from '@fluxstack/live'
export class Counter extends LiveComponent<{ count: number }> {
static componentName = 'Counter'
static defaultState = { count: 0 }
static publicActions = ['increment', 'decrement'] as const
async increment() {
this.setState({ count: this.state.count + 1 })
}
async decrement() {
this.setState({ count: this.state.count - 1 })
}
}
```
### Server Setup (server/index.ts)
```typescript
import { Elysia } from 'elysia'
import { LiveServer } from '@fluxstack/live'
import ElysiaTransport from '@fluxstack/live-elysia'
const app = new Elysia()
const server = new LiveServer({
transport: new ElysiaTransport(app),
componentsPath: './server/live',
})
await server.start()
app.listen(3000)
```
### Client (React)
```tsx
import { LiveComponentsProvider, Live } from '@fluxstack/live-react'
import type { Counter } from '@server/live/Counter'
function App() {
return (
<LiveComponentsProvider>
<CounterView />
</LiveComponentsProvider>
)
}
function CounterView() {
const counter = Live.use<typeof Counter>(Counter)
return (
<div>
<p>Count: {counter.count}</p>
<button onClick={() => counter.$call('increment')}>+</button>
<button onClick={() => counter.$call('decrement')}>-</button>
</div>
)
}
```
### Client (Vanilla JS)
```typescript
import { useLive } from '@fluxstack/live-client'
const counter = useLive('Counter', { count: 0 })
counter.on(state => {
document.getElementById('count')!.textContent = String(state.count)
})
document.querySelector('.inc')!.onclick = () => counter.call('increment')
```
## Key Concepts
### LiveComponent
Server-side component base class. Subclass it to define state, actions, and lifecycle hooks.
- **State**: Reactive `this.state` with proxy-based change detection. Use `this.setState(updates)` to push changes to all connected clients.
- **Actions**: Public methods listed in `static publicActions`. Called from clients via `$call('actionName', payload)`.
- **Lifecycle hooks**: `onMount()`, `onConnect()`, `onDisconnect()`, `onDestroy()`, `onStateChange()`, `onAction()`, `onRehydrate()`.
- **Private state**: `this.$private` — server-only state never sent to clients.
- **Rooms**: `this.$room.join()`, `this.$room.emit('event', data)`, `this.$room.on('event', handler)`.
- **Auth**: `this.$auth.hasRole('admin')`, with component-level and action-level authorization.
- **Binary deltas**: `this.sendBinaryDelta(delta, encoder)` for high-frequency state updates (games, real-time viz).
### Rooms
Multi-user pub/sub channels with shared state:
- Server: `this.$room.join()`, `this.$room.emit()`, `this.$room.on()`, `this.$room.setState()`
- Client: `proxy.$room.join()`, `proxy.$room.emit()`, `proxy.$room.on()`
- Cross-instance: Use `@fluxstack/live-redis` for horizontal scaling.
### Auth System
Pluggable auth providers. Supports JWT, API keys, or custom auth:
- Component-level: `static auth = { required: true, roles: ['admin'] }`
- Action-level: `static actionAuth = { deleteItem: { roles: ['admin'] } }`
- Custom provider: Implement `LiveAuthProvider` interface.
### Security
- HMAC-SHA256 state signing with nonce-based replay protection
- Payload sanitization against prototype pollution
- Token-bucket rate limiting per connection
- Message size limits, room count limits
- Origin validation (CSRF protection)
## For complete API reference, see llms-full.txt