Skip to content

Commit c6fb70f

Browse files
committed
Update README
1 parent 0148bf5 commit c6fb70f

1 file changed

Lines changed: 75 additions & 9 deletions

File tree

README.md

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,22 +253,88 @@ Violetear uses a hybrid strategy to ensure safety and speed:
253253
* **Push Notifications:** Not yet supported (requires VAPID key generation and a push server).
254254
* **Background Sync:** Offline actions (like submitting a form while disconnected) are not automatically retried when online. You must handle connection errors manually in your client logic.
255255

256-
## 🛣️ Roadmap
256+
Based on the `examples/broadcast.py` file you provided, here is a new section for the `README.md`.
257257

258-
We are currently in v1.1 (Core). Here is the vision for the immediate future of Violetear:
258+
This section explains the **Reverse RPC** capability (Server-to-Client communication), detailing how to define client-side functions and trigger them from the server to broadcast updates to all connected users.
259259

260-
### v1.2: The "App" Update (Deployment)
260+
You should place this section immediately after the **"🚀 Quickstart: The Isomorphic Counter"** section in your `README.md`.
261261

262-
* [x] **📱 Progressive Web Apps (PWA)**: Simply pass `@app.route(..., pwa=True)` to automatically generate `manifest.json` and a Service Worker.
263-
* [ ] **🔥 JIT CSS**: An optimization engine that scans your Python code and serves *only* the CSS rules actually used by your components.
262+
## 📡 Real-Time: Server Broadcasts
264263

265-
### v1.3: The "Navigation" Update (SPA)
264+
Violetear supports **Reverse RPC**, allowing the server to call functions running in the user's browser. This is perfect for real-time notifications, live feeds, or multiplayer games.
266265

267-
* [ ] **🧭 SPA Engine**: An abstraction (`violetear.spa`) for building Single Page Applications.
268-
* [ ] **🔀 Client-Side Routing**: Define routes that render specific components into a shell without reloading the page.
266+
The magic happens via the `.broadcast()` method available on any `@app.client` function.
267+
268+
### 1. Define the Client Function
269+
270+
Create a function decorated with `@app.client`. This code will be compiled and run in the browser, but the server "knows" about it.
271+
272+
```python
273+
# This function runs in the User's Browser
274+
@app.client
275+
async def update_alert(message: str, color: str):
276+
from violetear.dom import Document
277+
278+
# Update the DOM immediately
279+
el = Document.find("status-message")
280+
el.text = message
281+
el.style(color=color)
282+
```
283+
284+
### 2. Call it from the Server
285+
286+
You can call this function from anywhere in your server-side code (a background task, a cron job, or another API route) using `.broadcast()`.
287+
288+
```python
289+
import asyncio
290+
from contextlib import asynccontextmanager
291+
292+
# A background task running on the server
293+
async def background_monitor():
294+
while True:
295+
await asyncio.sleep(5)
296+
# Send data to ALL connected clients
297+
await update_alert.broadcast(
298+
message="Server is alive!",
299+
color="green"
300+
)
301+
302+
# Register the background task using standard FastAPI lifespan
303+
@asynccontextmanager
304+
async def lifespan(api):
305+
task = asyncio.create_task(background_monitor())
306+
yield
307+
task.cancel()
308+
309+
# Hook into the app
310+
app.api.router.lifespan_context = lifespan
311+
```
312+
313+
### 3. Handle Connections
314+
315+
You can also hook into WebSocket lifecycle events to track users or trigger actions when they join or leave.
316+
317+
```python
318+
@app.connect
319+
async def on_join(client_id: str):
320+
print(f"Client {client_id} connected.")
321+
# You could broadcast a "User Joined" message here
322+
await update_alert.broadcast(f"User {client_id} joined!", "blue")
323+
324+
@app.disconnect
325+
async def on_leave(client_id: str):
326+
print(f"Client {client_id} left.")
327+
```
328+
329+
## 🛣️ Roadmap
269330

270-
### v1.4: The "Twin-State" Update (Reactivity)
331+
Here is the vision for the immediate future of Violetear:
271332

333+
* [x] **📱 Progressive Web Apps (PWA)**: Simply pass `@app.route(..., pwa=True)` to automatically generate `manifest.json` and a Service Worker.
334+
* [x] **📡 Reverse RPC (Broadcast and Invoke)**: Invoke client-side functions from the server via websockets.
335+
* [ ] **🔥 JIT CSS**: An optimization engine that scans your Python code and serves *only* the CSS rules actually used by your components.
336+
* [ ] **🧭 SPA Engine**: An abstraction (`violetear.spa`) for building Single Page Applications.
337+
* [ ] **🔀 Client-Side Routing**: Define routes that render specific components into a shell without reloading the page.
272338
* [ ] **🗃️ `@app.local`**: Reactive state that lives in the browser (per user). Changes update the DOM automatically.
273339
* [ ] **🌐 `@app.shared`**: Real-time state that lives on the server (multiplayer). Changes are synced to all connected clients via **WebSockets**.
274340

0 commit comments

Comments
 (0)