Frontend -
ping.headbanger.me
Backend -
chat-app-lzp5.onrender.com
This document provides a comprehensive overview of the Ping Chat Application, including setup instructions, features, and technical implementation details.
The application consists of two parts: a Backend API/WebSocket server and a Frontend client. Both must be running for the application to function correctly.
- Node.js (Version 16 or higher)
- Redis Server (Running locally or accessible via cloud)
- Navigate to the backend directory:
cd backend - Install dependencies:
npm install
- Configure environment variables:
- Create a
.envfile based on.env.example. - Ensure Redis connection details are correct.
- Create a
- Start the server:
The backend runs on port 3000 by default.
npm run dev
- Open a new terminal and navigate to the frontend directory:
cd frontend - Install dependencies:
npm install
- Start the application:
The frontend runs on port 3000 (usually proxied to a different port if backend is taking 3000, e.g., 3001) or as configured. Check the console output for the local URL.
npm run dev
The real-time communication functionality is implemented using Socket.IO.
- Connection: When a client enters a room, a WebSocket connection is established. The user joins a specific "room" channel identified by the
roomId. - Event-Driven Communication: Data is transferred via named events.
join-room: Sent by client to authenticate and subscribe to room updates.send-message: Sent by client to push new content.new-message: Broadcast by server to all subscribers of the room.
- State Management: While Socket.IO requires a persistent connection, the application state (users, room metadata) is managed in memory on the server instance, with persistent data (message history) offloaded to Redis. This allows for quick state recovery and potentially scaling across multiple instances using a Redis adapter if needed in the future.
To maintain performance and manage resources, each chat room is strictly limited to 50 active messages. This is enforced on the backend before any new message is processed:
- Pre-Validation: When a
send-messageevent is received, the server queries Redis for the current message count of the specific room. - Conditional Logic:
- If the count is less than 50, the message is accepted, stored in Redis, and broadcast to the room. The counter is then incremented.
- If the count is equal to or greater than 50, the server rejects the message. It immediately emits a
limit-reachedevent back to the sender (or the room), preventing the addition of new data.
- UI Synchronization: The frontend listens for the
limit-reachedevent to disable the input field and notify the user that the conversation limit has been met.
Beyond standard real-time messaging, the application includes several advanced capabilities:
- Secure Authentication: A passwordless login system using Email OTPs (One-Time Passwords) ensures verified user identity without the hassle of managing passwords.
- Rich Media & Stickers: Integrated support for the Tenor API allows users to search for and send animated stickers, enhancing expressiveness.
- Real-Time Presence: Users can see "typing..." indicators when others are composing messages, along with a live list of active participants in the room.
- Session Persistence: Redis-backed session management allows users to refresh the page or reconnect without losing their logged-in state.
- Smart Message Grouping: To keep the chat clean and readable, consecutive messages from the same user are visually grouped, displaying the user's avatar only once for the entire block.
- Responsive Interface: The user interface is fully responsive, optimized for a seamless experience across desktop and mobile devices.
