Next.js Discord

Discord Forum

Chat System with DMs - DMs Disappearing, Slow Loading, Pusher Issues

Unanswered
Fan-tailed Warbler posted this in #help-forum
Open in Discord
Fan-tailed WarblerOP
What I'm trying to accomplish:
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 messages
Pusher not working properly (auth errors, events not triggering)
State management issues: polling overwrites DM list

What I've tried:
DM persistence with localStorage (dm-list-persistence.ts)
Save DM list and merge with server data
Instant loading from cache
Pusher optimization
Singleton client (pusher-client.ts)
Fixed auth endpoint
Better error handling
Performance optimizations
Optimized MongoDB queries (projections, smart collection scanning)
Only scan last 7 days for recent messages
Parallel profile fetches
State management fixes
Polling no longer overwrites DM list
Merge mode for safe updates
Optimistic updates with persistence

Current code structure:
src/app/chat/page.tsx - Main chat component
src/app/api/chat/dms/list/route.ts - DM list API
src/app/api/chat/dms/route.ts - DM messages API
src/app/hooks/usePusherChat.ts - Pusher hook
src/app/utils/dm-list-persistence.ts - DM persistence utility
src/app/utils/pusher-client.ts - Pusher singleton

Question:
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?

0 Replies