Advanced
HotWire Protocol
Real-time streaming without WebSocket complexity. Send partial HTML updates and JSON events using Server-Sent Events.
SSE-Based
No WebSocket setup required, works over HTTP
HTML Streaming
Send partial HTML updates to specific elements
Channels
Subscribe clients to channels for targeted updates
Auto-Reconnect
Built-in connection keepalive and recovery
Setting Up HotWire
app.ts
1import { createApp, hotWire } from "canxjs";23const app = createApp({ port: 3000 });45// Create HotWire SSE endpoint6app.get("/hotwire/connect", (req, res) => {7return hotWire.createStream(req, res);8});910// Subscribe to channels via POST11app.post("/hotwire/subscribe", async (req, res) => {12const { clientId, channel } = await req.body();13hotWire.subscribe(clientId, channel);14return res.json({ subscribed: channel });15});1617app.listen();
Broadcasting Events
broadcast.ts
1import { hotWire } from "canxjs";23// Broadcast JSON data to a channel4hotWire.broadcast("notifications", {5type: "new_message",6message: "Hello World!",7timestamp: Date.now()8});910// Broadcast to all connected clients11hotWire.broadcastAll({12type: "announcement",13text: "Server maintenance in 5 minutes"14});1516// Send to specific client17hotWire.sendTo(clientId, { type: "private", data: "secret" });
HTML Updates
Send partial HTML to update specific elements without full page reload.
html-updates.ts
1import { hotWire } from "canxjs";23// Send partial HTML update to channel4hotWire.broadcastHTML(5"dashboard", // channel6"<li>New item!</li>", // HTML content7"#notifications-list", // target selector8"append" // action: replace, append, prepend9);1011// Replace content12hotWire.broadcastHTML(13"users",14"<span class='status online'>Online</span>",15"#user-status-123",16"replace"17);
Client-Side Integration
client.js
1// Client-side JavaScript2const eventSource = new EventSource("/hotwire/connect");34eventSource.onopen = () => {5console.log("Connected to HotWire");6};78eventSource.onmessage = (event) => {9const data = JSON.parse(event.data);1011// Handle HTML updates12if (data.type === "html") {13const target = document.querySelector(data.target);14if (target) {15switch (data.action) {16case "replace": target.innerHTML = data.content; break;17case "append": target.innerHTML += data.content; break;18case "prepend": target.innerHTML = data.content + target.innerHTML; break;19}20}21}2223// Handle other events24console.log("Received:", data);25};2627eventSource.onerror = () => {28console.log("Connection lost, reconnecting...");29};
Real-World Example
notifications.ts
1// Real-time notifications example2app.post("/posts", authMiddleware, async (req, res) => {3const post = await Post.create(await req.body());45// Notify all subscribers6hotWire.broadcastHTML(7"feed",8`<article class="post">9<h3>${post.title}</h3>10<p>${post.excerpt}</p>11</article>`,12"#feed-container",13"prepend"14);1516// Also send JSON event17hotWire.broadcast("notifications", {18type: "new_post",19postId: post.id,20title: post.title21});2223return res.json(post);24});