Nexanexa
Step 11

Realtime: SSE and WebSocket

connectSSE and connectSocket in @nexa/http wrap the browser's native APIs in reactive signals — no proprietary protocol.

Server-Sent Events

import { connectSSE } from "@nexa/http";

const notifications = connectSSE({ url: "/events" });
// notifications.data.value -> the last message

data is the last message received, parsed as JSON when possible.

WebSocket

import { connectSocket } from "@nexa/http";

const chat = connectSocket({ url: "wss://myapp.com/chat" });
chat.send({ text: "hi" });
// chat.status.value -> "connecting" | "open" | "reconnecting" | "closed"

status reflects the connection's real lifecycle; send() automatically serializes objects as JSON.

Real reconnection, with backoff

const chat = connectSocket({
    url: "wss://myapp.com/chat",
    reconnect: { maxAttempts: 10, baseDelayMs: 500, maxDelayMs: 15000 }
});

If the connection drops, reconnect: true (or a { maxAttempts, baseDelayMs, maxDelayMs } object to tune it) makes it reconnect on its own with exponential backoff + jitter — status goes through "reconnecting" while it tries. Verified by killing and restarting a real WebSocket server while a real browser kept the connection open. Without that option, it stays the same as always: status goes to "closed" and stays there.