import { io, type Socket } from 'socket.io-client'; let socket: Socket | null = null; export function getSocket(): Socket { if (!socket) { socket = io(window.location.origin, { path: '/ws', autoConnect: false, withCredentials: true, reconnection: true, reconnectionAttempts: Infinity, reconnectionDelay: 1000, reconnectionDelayMax: 5000, }); } return socket; } export function connectSocket(): void { const s = getSocket(); if (!s.connected) { // Socket will authenticate via cookie (token is sent as cookie in the handshake) s.connect(); } } export function disconnectSocket(): void { if (socket?.connected) { socket.disconnect(); } }