Install the package and peer WebSocket dependencies into your modern TypeScript or JavaScript project.
Attach subatomPulse to an existing HTTP server to cleanly share TLS termination and health routes on the same port.
1import http from "node:http";1import { subatomPulse } from "subatom-pulse";11const httpServer = http.createServer((req, res) => {1 if (req.url === "/health") {1 res.writeHead(200, { "content-type": "application/json" });1 res.end(JSON.stringify({ status: "healthy", timestamp: Date.now() }));1 return;1 }1 res.writeHead(404);1 res.end();1});21// Attach subatom-pulse directly to HTTP server upgrade pipeline1const io = subatomPulse({1 server: httpServer,1 path: "/ws",1 maxConnections: 10_000,1 rateLimitPerSec: 50,1 overflowPolicy: "drop-oldest",1 authenticator: async (req) => {1 const url = new URL(req.url ?? "/", `http://${req.headers.host}`);1 const token = url.searchParams.get("token");1 const userId = url.searchParams.get("user") ?? "guest";31 if (!token || token !== "Bearer secret-app-token") {1 return { authenticated: false };1 }1 return { authenticated: true, userId, metadata: { userId, role: "member" } };2 }2});41io.onConnection((socket) => {1 console.log(`[Connection Opened] ID: ${socket.id} for User: ${socket.metadata.userId}`);51 // Greet new connection1 socket.emit("system.welcome", { id: socket.id, time: Date.now() });3});61// Idempotent SIGTERM/SIGINT teardown orchestration1io.installSignalHandlers();1httpServer.listen(3000, () => console.log("Server active on :3000"));
The client runs natively in browsers. If testing inside Node.js, inject ws into globalThis.WebSocket.
1import { SubAtomPulse } from "subatom-pulse";11// Note: If running inside Node.js, inject native WebSocket into globalThis:1// import { WebSocket } from "ws"; globalThis.WebSocket = WebSocket as any;21const socket = new SubAtomPulse("ws://localhost:3000/ws", {1 auth: { token: "Bearer secret-app-token" },1 reconnect: true,1 reconnectAttempts: 10,1 reconnectDelayMs: 1500,1 autoConnect: true1});31socket.on("connect", () => {1 console.log("[WS Connected] Active state:", socket.state);2});41socket.on("system.welcome", (data) => {1 console.log("Handshake confirmation:", data);3});51socket.on("error", (err) => {1 console.error("Socket Protocol Error:", err);4});
SubAtomPulse will automatically retry dropped connections every 1500ms by default. Buffered packets sent while offline flush as soon as the socket reopens.