Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 92 additions & 23 deletions server/src/sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,45 @@ interface FileShareSession {
// Ephemeral by nature (they reference live sockets / in-browser blobs).
const fileShares = new Map<string, FileShareSession>()
const roomFileShares = new Map<string, RoomFileShare>()
const onlineUsers = new Map<string, { socketId: string; username: string }>()
// A user can be connected from several tabs/devices at once. Track the full
// set of sockets per user; presence flips only on the first/last one.
const userSockets = new Map<string, Map<string, string>>() // userId -> socketId -> username

function socketsOf(userId: string): string[] {
return [...(userSockets.get(userId)?.keys() ?? [])]
}
function isOnline(userId: string): boolean {
return (userSockets.get(userId)?.size ?? 0) > 0
}
/** Most recently connected socket for a user (Map keeps insertion order). */
function newestSocket(userId: string): string | null {
const m = userSockets.get(userId)
if (!m || m.size === 0) return null
let last: string | null = null
for (const sid of m.keys()) last = sid
return last
}

// 1:1 P2P file signaling (dm-file-*, group-file-*) is a handshake between two
// specific sockets, but dm-file-signal carries no transferId to route by. So
// we pin, per unordered user pair, which socket of each user is engaged — set
// as each side reveals itself (offer/request/signal). Limitation: one active
// transfer per peer pair per device; a second simultaneous transfer with the
// same peer from another device would retarget the pin.
const filePeerPins = new Map<string, Record<string, string>>()
function pairKey(a: string, b: string): string {
return a < b ? `${a}|${b}` : `${b}|${a}`
}
function pinPeer(self: string, other: string, socketId: string): void {
const k = pairKey(self, other)
const rec = filePeerPins.get(k) ?? {}
rec[self] = socketId
filePeerPins.set(k, rec)
}
/** The other user's pinned socket for this pair, or their newest as fallback. */
function pinnedSocket(self: string, other: string): string | null {
return filePeerPins.get(pairKey(self, other))?.[other] ?? newestSocket(other)
}
const voiceRooms = new Map<string, Map<string, string>>() // roomId -> socketId -> username
const streamRequests = new Map<string, { roomId: string; url: string; fromUserId: string }>() // requestId -> request
// `${socketId}:${roomId}` -> join timestamp, for accruing watch time on leave.
Expand Down Expand Up @@ -59,8 +97,8 @@ export function emitToUsers<E extends keyof ServerToClientEvents>(
if (!ioRef) return
const seen = new Set<string>()
for (const uid of userIds) {
const sid = onlineUsers.get(uid)?.socketId
if (sid && !seen.has(sid)) {
for (const sid of socketsOf(uid)) {
if (seen.has(sid)) continue
seen.add(sid)
ioRef.to(sid).emit(event, ...args)
}
Expand Down Expand Up @@ -102,8 +140,15 @@ export default function initSocket(server: HttpServer, allowOrigin: (origin: str
io.on('connection', (rawSocket) => {
const socket = rawSocket as BeaconSocket
const { id: userId, username, isGuest } = socket.data.user
onlineUsers.set(userId, { socketId: socket.id, username })
io.emit('user-online', { id: userId, username })
let sockets = userSockets.get(userId)
if (!sockets) {
sockets = new Map()
userSockets.set(userId, sockets)
}
const wasOffline = sockets.size === 0
sockets.set(socket.id, username)
// Announce presence only when the user's first device connects.
if (wasOffline) io.emit('user-online', { id: userId, username })

/** Register a handler whose payload is validated against its zod schema first. */
function on<E extends keyof SchemaMap>(event: E, handler: (data: z.infer<SchemaMap[E]>) => void) {
Expand Down Expand Up @@ -227,17 +272,19 @@ export default function initSocket(server: HttpServer, allowOrigin: (origin: str
} catch (e) {
console.error('Failed to persist private message:', e)
}
const rec = onlineUsers.get(to)
if (rec) io.to(rec.socketId).emit('private-message', { from: { id: userId, username }, message, timestamp: ts })
// Deliver to every device the recipient has open.
for (const sid of socketsOf(to)) {
io.to(sid).emit('private-message', { from: { id: userId, username }, message, timestamp: ts })
}
bumpStat(userId, 'messages_sent', 1)
})

// ---- File-in-DM (P2P transfer between two accounts, relayed by userId) ----
function dmRecipientSocket(to: string): string | null {
if (isGuest) return null
// Guards a peer id for file signaling: must be a real, non-guest account.
function validPeer(to: string): boolean {
if (isGuest) return false
const recipient = findUserById(to)
if (!recipient || recipient.is_guest) return null
return onlineUsers.get(to)?.socketId ?? null
return !!recipient && !recipient.is_guest
}
on('dm-file-offer', ({ to, transferId, fileInfo }) => {
if (isGuest) return
Expand All @@ -250,19 +297,28 @@ export default function initSocket(server: HttpServer, allowOrigin: (origin: str
} catch (e) {
console.error('Failed to persist file offer:', e)
}
const sid = onlineUsers.get(to)?.socketId
if (sid) io.to(sid).emit('dm-file-offer', { from: userId, fromUsername: username, transferId, fileInfo })
// The sender's socket is one end of the handshake; pin it for this pair.
pinPeer(userId, to, socket.id)
// Any of the recipient's devices may accept, so offer them all.
for (const sid of socketsOf(to)) {
io.to(sid).emit('dm-file-offer', { from: userId, fromUsername: username, transferId, fileInfo })
}
})
on('dm-file-request', ({ to, transferId }) => {
const sid = dmRecipientSocket(to)
if (!validPeer(to)) return
pinPeer(userId, to, socket.id) // accepting device is our end
const sid = pinnedSocket(userId, to)
if (sid) io.to(sid).emit('dm-file-request', { from: userId, transferId })
})
on('dm-file-decline', ({ to, transferId }) => {
const sid = dmRecipientSocket(to)
if (!validPeer(to)) return
const sid = pinnedSocket(userId, to)
if (sid) io.to(sid).emit('dm-file-decline', { from: userId, transferId })
})
on('dm-file-signal', ({ to, signal }) => {
const sid = dmRecipientSocket(to)
if (!validPeer(to)) return
pinPeer(userId, to, socket.id)
const sid = pinnedSocket(userId, to)
if (sid) io.to(sid).emit('dm-file-signal', { from: userId, signal })
})
on('dm-party-invite', ({ to, roomId }) => {
Expand All @@ -276,8 +332,9 @@ export default function initSocket(server: HttpServer, allowOrigin: (origin: str
} catch (e) {
console.error('Failed to persist party invite:', e)
}
const sid = onlineUsers.get(to)?.socketId
if (sid) io.to(sid).emit('dm-party-invite', { from: userId, fromUsername: username, roomId })
for (const sid of socketsOf(to)) {
io.to(sid).emit('dm-party-invite', { from: userId, fromUsername: username, roomId })
}
})

// ---- Group DMs ----
Expand Down Expand Up @@ -311,11 +368,15 @@ export default function initSocket(server: HttpServer, allowOrigin: (origin: str
// Group file transfer handshake (1:1 between sharer and each downloader),
// relayed by userId on its own channel so it never collides with dm-file-*.
on('group-file-request', ({ to, transferId }) => {
const sid = dmRecipientSocket(to)
if (!validPeer(to)) return
pinPeer(userId, to, socket.id)
const sid = pinnedSocket(userId, to)
if (sid) io.to(sid).emit('group-file-request', { from: userId, transferId })
})
on('group-file-signal', ({ to, signal }) => {
const sid = dmRecipientSocket(to)
if (!validPeer(to)) return
pinPeer(userId, to, socket.id)
const sid = pinnedSocket(userId, to)
if (sid) io.to(sid).emit('group-file-signal', { from: userId, signal })
})

Expand Down Expand Up @@ -427,9 +488,17 @@ export default function initSocket(server: HttpServer, allowOrigin: (origin: str
// ---- Disconnect ----

socket.on('disconnect', () => {
if (onlineUsers.get(userId)?.socketId === socket.id) {
onlineUsers.delete(userId)
io.emit('user-offline', userId)
const mySockets = userSockets.get(userId)
if (mySockets?.delete(socket.id) && mySockets.size === 0) {
userSockets.delete(userId)
io.emit('user-offline', userId) // last device gone
}
// Drop any file-signaling pins this socket held.
for (const [k, rec] of filePeerPins) {
for (const uid of Object.keys(rec)) {
if (rec[uid] === socket.id) delete rec[uid]
}
if (Object.keys(rec).length === 0) filePeerPins.delete(k)
}
rooms.handleDisconnect(
userId,
Expand Down
109 changes: 109 additions & 0 deletions tests/integration/multi-device.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { test, expect } from '@playwright/test'
import { spawn, type ChildProcess } from 'child_process'
import { io, type Socket } from 'socket.io-client'
import path from 'path'
import fs from 'fs'
import os from 'os'

// A single account can be connected from several devices at once. This proves
// the server-side session set: a DM reaches every device, and presence flips
// off only when the last device disconnects — not when any one does.

const PORT = 3211
const BASE = `http://localhost:${PORT}`
const ROOT = path.resolve(__dirname, '..', '..')
const SERVER_DIR = path.join(ROOT, 'server')
const TSX = path.join(SERVER_DIR, 'node_modules', 'tsx', 'dist', 'cli.mjs')

let serverProc: ChildProcess | null = null
let dbPath: string

function startServer(): Promise<void> {
serverProc = spawn(process.execPath, [TSX, 'src/index.ts'], {
cwd: SERVER_DIR,
env: { ...process.env, PORT: String(PORT), JWT_SECRET: 'multi-device-test-secret', DB_PATH: dbPath },
stdio: 'ignore',
})
return waitForHealth()
}

async function waitForHealth(): Promise<void> {
for (let i = 0; i < 100; i++) {
try { if ((await fetch(`${BASE}/api/health`)).ok) return } catch { /* not up */ }
await new Promise((r) => setTimeout(r, 200))
}
throw new Error('Server did not become healthy in time')
}

function stopServer(): Promise<void> {
return new Promise((resolve) => {
if (!serverProc) return resolve()
serverProc.once('exit', () => resolve())
serverProc.kill()
serverProc = null
})
}

function connect(token: string): Promise<Socket> {
return new Promise((resolve, reject) => {
const s = io(BASE, { auth: { token }, transports: ['websocket'] })
s.once('connect', () => resolve(s))
s.once('connect_error', reject)
})
}

async function signup(name: string): Promise<{ token: string; id: string }> {
const res = await fetch(`${BASE}/api/auth/signup`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: name, email: `${name.toLowerCase()}@md.test`, password: 'hunter22' }),
})
expect(res.status).toBe(201)
const data = await res.json()
return { token: data.token, id: data.user.id }
}

test.describe('multi-device sessions', () => {
test.beforeAll(async () => {
dbPath = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'beacon-md-')), 'test.db')
await startServer()
})
test.afterAll(async () => { await stopServer() })

test('a DM reaches every device the recipient has open, and presence tracks the last one', async () => {
const tag = Date.now().toString(36)
const alice = await signup(`AliceMD${tag}`)
const bob = await signup(`BobMD${tag}`)

// Bob on two devices; Alice on one.
const bob1 = await connect(bob.token)
const bob2 = await connect(bob.token)
const aliceSock = await connect(alice.token)

// Alice should see exactly one user-online for Bob across his two
// connections (presence announced only on the first device).
let bobOnlineCount = 0
aliceSock.on('user-online', (u: { id: string }) => { if (u.id === bob.id) bobOnlineCount++ })

// Both of Bob's devices receive the DM.
const got1 = new Promise<string>((res) => bob1.once('private-message', (m: any) => res(m.message)))
const got2 = new Promise<string>((res) => bob2.once('private-message', (m: any) => res(m.message)))
aliceSock.emit('private-message', { to: bob.id, message: 'ping-both', timestamp: Date.now() })
expect(await got1).toBe('ping-both')
expect(await got2).toBe('ping-both')

// Presence goes offline only after BOTH of Bob's devices drop.
let bobOffline = false
aliceSock.on('user-offline', (id: string) => { if (id === bob.id) bobOffline = true })

bob1.close()
await new Promise((r) => setTimeout(r, 400))
expect(bobOffline).toBe(false) // bob2 still open

bob2.close()
await new Promise((r) => setTimeout(r, 400))
expect(bobOffline).toBe(true) // last device gone

aliceSock.close()
})
})
Loading