Lobby Frontend Updates for Global Chat
Overview
The lobby frontend components have been updated to support global chat functionality alongside the existing room-based chat. This allows for seamless integration of global chat features while maintaining backward compatibility with existing room chat implementations.
Updated Components
1. EnhancedChatLobby Component
Location: src/components/game/enhanced-chat-lobby.tsx
New Props:
isGlobalChat?: boolean- Enables global chat mode when set totrue
Key Changes:
- Dynamic WebSocket URL: Automatically switches between global chat and room chat endpoints
- Message Type Handling: Supports both
global_messageand regularchat_messagetypes - Join Message Types: Uses
player_join_globalfor global chat,joinfor room chat - Connection Messages: Different success messages for global vs room chat
Usage Example:
// Room chat (existing behavior)
<EnhancedChatLobby
lobbyId="room-123"
player={player}
isGlobalChat={false}
/>
// Global chat (new functionality)
<EnhancedChatLobby
lobbyId="global-chat"
player={player}
isGlobalChat={true}
/>
2. ChatLobby Component
Location: src/components/game/chat-lobby.tsx
New Props:
isGlobalChat?: boolean- Enables global chat mode when set totrue
Key Changes:
- WebSocket Endpoint: Routes to
/api/global-chatwhenisGlobalChatis true - Message Handling: Supports global chat message types
- NPC Interactions: Maintains NPC functionality for both modes
Usage Example:
// Room chat
<ChatLobby
lobbyId="room-123"
player={player}
isGlobalChat={false}
/>
// Global chat
<ChatLobby
lobbyId="global-chat"
player={player}
isGlobalChat={true}
/>
3. GlobalChatLobby Component (New)
Location: src/components/game/global-chat-lobby.tsx
Features:
- Dedicated Global Chat UI: Specialized interface for global chat
- Minimizable Interface: Can be minimized or fully collapsed
- Global Statistics: Displays platform-wide statistics
- Responsive Design: Works on both desktop and mobile
Props:
player: PlayerInfo- Player informationautoConnect?: boolean- Auto-connect to global chatisModal?: boolean- Display as modal overlayhideHeader?: boolean- Hide the header section
Usage Example:
<GlobalChatLobby
player={player}
autoConnect={true}
isModal={false}
hideHeader={false}
/>
Message Type Support
Global Chat Message Types
| Message Type | Description | Component Support |
|---|---|---|
global_message | Regular global chat messages | ✅ All components |
player_join_global | Player joining global chat | ✅ All components |
welcome_global | Welcome message for new users | ✅ All components |
global_stats | Platform statistics updates | ✅ GlobalChatLobby |
global_player_list | Online players list | ✅ All components |
typing_start_global | Global typing indicators | ✅ All components |
typing_stop_global | Global typing indicators | ✅ All components |
level_up_global | Global level up announcements | ✅ All components |
Room Chat Message Types (Existing)
| Message Type | Description | Component Support |
|---|---|---|
chat_message | Regular room chat messages | ✅ All components |
join | Player joining room | ✅ All components |
npc_message | NPC responses | ✅ All components |
level_up | Room level up announcements | ✅ All components |
player_list | Room players list | ✅ All components |
Implementation Examples
1. Adding Global Chat to Existing Pages
Before (Room-only chat):
<EnhancedChatLobby
lobbyId={roomId}
player={player}
/>
After (With global chat option):
<EnhancedChatLobby
lobbyId={isGlobalChat ? "global-chat" : roomId}
player={player}
isGlobalChat={isGlobalChat}
/>
2. Creating a Global Chat Only Page
import { GlobalChatLobby } from '@/components/game/global-chat-lobby';
export default function GlobalChatPage() {
return (
<div className="min-h-screen bg-background">
<GlobalChatLobby
player={player}
autoConnect={true}
isModal={false}
/>
</div>
);
}
3. Toggle Between Room and Global Chat
import { useState } from 'react';
import { EnhancedChatLobby } from '@/components/game/enhanced-chat-lobby';
export function ChatToggle() {
const [isGlobalChat, setIsGlobalChat] = useState(false);
const [roomId, setRoomId] = useState('room-123');
return (
<div>
<div className="flex gap-2 mb-4">
<Button
onClick={() => setIsGlobalChat(false)}
variant={!isGlobalChat ? "default" : "outline"}
>
Room Chat
</Button>
<Button
onClick={() => setIsGlobalChat(true)}
variant={isGlobalChat ? "default" : "outline"}
>
Global Chat
</Button>
</div>
<EnhancedChatLobby
lobbyId={isGlobalChat ? "global-chat" : roomId}
player={player}
isGlobalChat={isGlobalChat}
/>
</div>
);
}
WebSocket Connection Logic
Global Chat Connection
// When isGlobalChat = true
const wsUrl = `${protocol}//${window.location.host}/api/global-chat`;
// Join message
ws.send(JSON.stringify({
type: 'player_join_global',
player: player
}));
// Send message
ws.send(JSON.stringify({
type: 'global_message',
message: message,
player: player
}));
Room Chat Connection
// When isGlobalChat = false
const wsUrl = `${protocol}//${window.location.host}/api/lobby/${lobbyId}/websocket`;
// Join message
ws.send(JSON.stringify({
type: 'join',
player: player
}));
// Send message
ws.send(JSON.stringify({
type: 'message',
message: message,
player: player
}));
Styling and UI Considerations
Global Chat Visual Indicators
GlobalChatLobby Component:
- Globe icon (🌐) to distinguish from room chat
- Blue color scheme for global chat elements
- Online player count badge
- Platform statistics display
EnhancedChatLobby/ChatLobby Components:
- Dynamic connection messages
- Context-aware join notifications
- Message type handling for both modes
Responsive Design
Desktop:
- Global chat can be displayed as sidebar or overlay
- Minimizable interface for non-intrusive experience
- Statistics panel for detailed information
Mobile:
- Full-width or modal display options
- Collapsible interface for space efficiency
- Touch-friendly controls
Migration Guide
For Existing Implementations
- No Breaking Changes: Existing room chat functionality remains unchanged
- Optional Global Chat: Add
isGlobalChatprop only when needed - Backward Compatibility: All existing props and functionality preserved
Adding Global Chat Support
-
Import Updated Components:
import { EnhancedChatLobby } from '@/components/game/enhanced-chat-lobby'; import { GlobalChatLobby } from '@/components/game/global-chat-lobby'; -
Add Global Chat Option:
<EnhancedChatLobby lobbyId="global-chat" player={player} isGlobalChat={true} /> -
Handle Global Message Types:
// Message handling is already included in updated components // No additional code needed for basic functionality
Best Practices
1. User Experience
- Provide clear visual distinction between global and room chat
- Use appropriate icons and colors for each chat type
- Implement smooth transitions between chat modes
2. Performance
- Lazy load global chat components when needed
- Implement proper cleanup for WebSocket connections
- Use efficient message handling for high-volume global chat
3. Accessibility
- Maintain keyboard navigation support
- Provide screen reader descriptions for chat types
- Ensure proper focus management
4. Error Handling
- Graceful fallback for connection failures
- Clear error messages for users
- Automatic reconnection for global chat
Future Enhancements
Planned Features
- Global Chat Channels: Multiple global chat channels
- Moderation Tools: Global chat moderation interface
- Advanced Statistics: Real-time analytics dashboard
- Integration Features: Cross-room notifications
Scalability Considerations
- Message Batching: Efficient handling of high-volume global chat
- Rate Limiting: Per-user rate limiting for global messages
- Content Filtering: AI-powered content moderation
- Performance Optimization: Message compression and caching
Conclusion
The lobby frontend updates provide a seamless integration of global chat functionality while maintaining full backward compatibility with existing room chat implementations. The modular approach allows developers to choose the appropriate component and configuration for their specific use case.
The updated components handle all the complexity of WebSocket connections, message routing, and UI state management, making it easy to add global chat support to any existing application.