Skip to content

Backend example

A WebSocket server with per-connection reactive state, an idle timeout, and an HTTP /stats endpoint that streams server-wide counts as Server-Sent Events. Runs as a long-lived Node service.

You can write this in either .pts (TypeScript superset) or .pjs (JavaScript superset). Both use the same runtime behavior. The tabs below sync — pick one and the rest of the page follows.

my-server/
├── src/server.pts
└── package.json
import { createServer } from "node:http";
import { effect as observe } from "@lyku/para-signals";
import { WebSocketServer, type WebSocket } from "ws";
signal totalConnections = 0;
signal totalMessages = 0;
const server = createServer((req, res) => {
if (req.url !== "/stats") return res.writeHead(404).end();
res.writeHead(200, {
"content-type": "text/event-stream",
"cache-control": "no-cache",
});
const stop = observe(() => {
res.write(`data: ${JSON.stringify({
active: totalConnections,
messages: totalMessages,
})}\n\n`);
});
res.on("close", stop);
});
const wss = new WebSocketServer({ server, path: "/ws" });
let nextId = 1;
wss.on("connection", (ws: WebSocket) => {
const id = nextId++;
totalConnections++;
signal messages = 0;
signal lastSeen = Date.now();
signal now = Date.now();
signal idle = (now - lastSeen) > 60_000;
let closed = false;
const tick = setInterval(() => { now = Date.now(); }, 1_000);
ws.on("message", buf => {
messages++;
totalMessages++;
lastSeen = Date.now();
ws.send(buf);
});
ws.on("close", () => {
closed = true;
clearInterval(tick);
stopIdle();
stopLog();
totalConnections--;
});
let wasIdle = false;
const stopIdle = observe(() => {
const isIdle = idle;
if (isIdle && !wasIdle && !closed) {
console.log(`[${id}] idle 60s, closing`);
ws.close(1000, "idle timeout");
}
wasIdle = isIdle;
});
const stopLog = observe(() => {
console.log(`[${id}] msgs=${messages} idle=${idle}`);
// First connection: [1] msgs=0 idle=false
// After its first message: [1] msgs=1 idle=false
});
});
const port = Number(process.env.PORT ?? 8080);
server.listen(port, "127.0.0.1", () => {
console.log("listening", server.address());
});
  • Module-level signals count connections and messages in this one server process. Each WebSocket has its own message count and idle clock.
  • The one-second timer updates now. The derived idle becomes true on the first tick more than 60 seconds after the last message. wasIdle makes the close action run only on a false-to-true transition.
  • observe is an alias for the published core effect function. It runs immediately and returns a disposer. The SSE response disposes its effect on close; each WebSocket clears its timer and disposes both effects on close.
  • The npm runtime used by Node does not export Parabun’s when or signalEvery helpers. This example therefore uses the core effect API and a normal timer while retaining lexical signal declarations. An effect { ... } statement cannot be assigned to a variable to capture a disposer.
{
"name": "my-server",
"type": "module",
"scripts": {
"build": "parabun build src/server.pts --target node --external ws --outfile dist/server.js",
"start": "node dist/server.js"
},
"dependencies": {
"ws": "8.21.0",
"@lyku/para-signals": "0.0.1-pre.4"
},
"devDependencies": { "@types/ws": "*" }
}

The build bundles the published signals runtime and leaves ws external. Keep the installed production dependencies alongside dist/server.js. No @lyku/para-parallel dependency is needed.

Terminal window
parabun install
parabun run build
node dist/server.js

Open http://127.0.0.1:8080/stats to watch SSE counts and connect a WebSocket client to ws://127.0.0.1:8080/ws. PORT changes the listening port. The example binds to loopback for local development; configure the listening interface and your reverse proxy when hosting it.

This is a long-lived Node process with open WebSocket/SSE connections and in-memory counters. Deploy it to a host that keeps that process running. It is not an AWS Lambda handler: Lambda needs an invocation handler and a separate WebSocket/event architecture.

Verified with Parabun 1.4.0-canary.1+117854cd6 (release parabun-117854cd62), Node 26.0.0, and the dependency versions above. Both language forms passed local HTTP/SSE and WebSocket tests. Controlled-clock checks verify idle timeout relative to the last message and cleanup. The install guide tracks newer releases; these results do not certify every compiler version.