Chat System with DMs - DMs Disappearing, Slow Loading, Pusher Issues
Unanswered
Fan-tailed Warbler posted this in #help-forum
Fan-tailed WarblerOP
What I'm trying to accomplish:
Main issues encountered:
What I've tried:
Current code structure:
Question:
Building a real-time chat system with global chat and direct messages (DMs), similar to Discord. Using Next.js 16, MongoDB, and Pusher for real-time updates.Main issues encountered:
DMs disappear after accepting an invite (toast shows success but DM doesn't appear)Slow loading times for DM list and messagesPusher not working properly (auth errors, events not triggering)State management issues: polling overwrites DM listWhat I've tried:
DM persistence with localStorage (dm-list-persistence.ts)Save DM list and merge with server dataInstant loading from cachePusher optimizationSingleton client (pusher-client.ts)Fixed auth endpointBetter error handlingPerformance optimizationsOptimized MongoDB queries (projections, smart collection scanning)Only scan last 7 days for recent messagesParallel profile fetchesState management fixesPolling no longer overwrites DM listMerge mode for safe updatesOptimistic updates with persistenceCurrent code structure:
src/app/chat/page.tsx - Main chat componentsrc/app/api/chat/dms/list/route.ts - DM list APIsrc/app/api/chat/dms/route.ts - DM messages APIsrc/app/hooks/usePusherChat.ts - Pusher hooksrc/app/utils/dm-list-persistence.ts - DM persistence utilitysrc/app/utils/pusher-client.ts - Pusher singletonQuestion:
Is there a better approach for this? Any Next.js-specific patterns I should use? Or should I test first and report back if issues persist?