In high-performance WebSocket engines, sockets should never store direct in-memory references to other sockets. Instead, subatom-pulse leverages Deterministic Private Rooms.
1. When user user_101 connects, server invokes socket.join("user:user_101").
2. To message user_101, another socket emits to room socket.to("user:user_101").emit(...).
3. If a user has multiple active tabs or devices, all of them belong to the same room set and receive the message concurrently.
1// SERVER: Register private user inbox room on connection1io.onConnection((socket) => {1 const userId = String(socket.metadata.userId);1 const privateInbox = `user:${userId}`;11 // Auto-subscribe socket to their personal room1 socket.join(privateInbox);21 // Listen for direct message dispatch1 socket.on("message.direct", (data: { toUserId?: string; text?: string }, ack) => {1 if (!data?.toUserId || !data?.text) {1 return ack?.({ ok: false, error: "Recipient 'toUserId' and 'text' required" });1 }31 // Deliver strictly to the recipient's personal room1 const targetRoom = `user:${data.toUserId}`;1 socket.to(targetRoom).emit("chat.direct", {1 fromUserId: userId,1 text: data.text,1 sentAt: Date.now()1 });41 // Acknowledge receipt to sender1 ack?.({ ok: true, deliveredAt: Date.now() });1 });1});51// CLIENT: Alice sends a message to Bob1const alice = new SubAtomPulse("ws://localhost:3000/ws?user=alice&token=Bearer%20token");61alice.on("connect", async () => {1 const receipt = await alice.emitWithAck("message.direct", {1 toUserId: "bob",1 text: "Reviewing the PR now!"2 });1 console.log("Delivered to Bob's room:", receipt);2});