UNPKG

socket.io-react-hooks-advanced

Version:

A modular and extensible React + Socket.IO hook library designed for real-world applications. Supports namespaced sockets, reconnection strategies, offline queues, latency monitoring, middleware, encryption, and more.

29 lines (28 loc) 911 B
const DEFAULT_KEY = "socketio.queue"; const DEFAULT_TTL = 15 * 60 * 1000; // 15 minutes export const saveQueue = (queue, key = DEFAULT_KEY) => { try { const json = JSON.stringify(queue.map(({ ack, ...rest }) => rest)); localStorage.setItem(key || DEFAULT_KEY, json); // <-- fallback here } catch (err) { console.error("Failed to persist emit queue:", err); } }; export const loadQueue = (key = DEFAULT_KEY, ttl = DEFAULT_TTL) => { try { const json = localStorage.getItem(key); if (!json) return []; const now = Date.now(); const parsed = JSON.parse(json); return parsed.filter((item) => now - item.timestamp < ttl); } catch (err) { console.warn("Failed to load queued emits:", err); return []; } }; export const clearQueue = (key = DEFAULT_KEY) => { localStorage.removeItem(key); };