You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+75-9Lines changed: 75 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -253,22 +253,88 @@ Violetear uses a hybrid strategy to ensure safety and speed:
253
253
***Push Notifications:** Not yet supported (requires VAPID key generation and a push server).
254
254
***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.
255
255
256
-
## 🛣️ Roadmap
256
+
Based on the `examples/broadcast.py` file you provided, here is a new section for the `README.md`.
257
257
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.
259
259
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`.
261
261
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
264
263
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.
266
265
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
+
asyncdefupdate_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
+
asyncdefbackground_monitor():
294
+
whileTrue:
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
+
asyncdeflifespan(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
+
asyncdefon_join(client_id: str):
320
+
print(f"Client {client_id} connected.")
321
+
# You could broadcast a "User Joined" message here
0 commit comments