Node.js Real-Time Chat Application is a simple yet powerful real-time chat application built using Node.js, Express, and Socket.io. This project demonstrates real-time communication between clients and server using WebSockets, allowing users to chat instantly without page refreshes.
Perfect for developers looking to understand:
- Real-time web applications
- Socket.io for WebSocket communication
- Basic Node.js server architecture
- Frontend-backend integration
- Real-time messaging - Instant message delivery to all connected users
- User management - Unique usernames for each participant
- Simple UI - Clean, responsive interface using Bootstrap 5
- Cross-browser compatibility - Works on all modern browsers
- Easy deployment - Simple setup with Node.js
- Scalable architecture - Ready for expansion with additional features
- Backend: Node.js, Express, Socket.io
- Frontend: HTML5, CSS3, Bootstrap 5
- Real-time Communication: WebSockets via Socket.io
- Development Environment: Node.js 12+
- Package Manager: npm
Ensure you have the following installed:
-
Clone the repository:
git clone https://github.com/aadhar-gaur/nodejs-chat-app.git cd nodejs-chat-app -
Install dependencies:
npm install
-
Start the server:
node server.js
-
Open the application:
- Open your browser and navigate to
http://localhost:3000
- Open your browser and navigate to
For development, you can use:
npm run devThis will start the server with nodemon for automatic restarting during development.
- Access the application in your browser at
http://localhost:3000 - Create a username by entering a unique name and clicking "Submit"
- Start chatting by typing messages in the input field and pressing Enter
- See all messages in real-time as they are sent by other users
// server.js
io.sockets.on("connection", function(socket) {
// Handle new user connection
socket.on("new user", function(data, callback) {
if (usernames.indexOf(data) != -1) {
callback(false); // Username taken
} else {
callback(true);
socket.username = data;
usernames.push(socket.username);
updateUsernames();
}
});
// Handle message sending
socket.on("send message", function(data) {
io.sockets.emit("new message", {msg: data, user: socket.username});
});
// Handle user disconnection
socket.on("disconnect", function() {
if (!socket.username) return;
usernames.splice(usernames.indexOf(socket.username), 1);
updateUsernames();
});
});// Inside your client-side JavaScript
const socket = io();
socket.on('connect', () => {
console.log('Connected to server');
});
socket.on('new message', (data) => {
// Display received message
const messageElement = document.createElement('div');
messageElement.textContent = `${data.user}: ${data.msg}`;
document.getElementById('chatWindow').appendChild(messageElement);
});nodejs-chat-app/
βββ node_modules/ # Dependencies
βββ public/ # Static files
β βββ css/ # CSS files
β βββ js/ # JavaScript files
β βββ index.html # Main HTML file
βββ views/ # View templates (if any)
βββ .gitignore # Git ignore file
βββ package.json # Project dependencies and scripts
βββ package-lock.json # Lock file for dependency versions
βββ README.md # This file
βββ server.js # Main server file
You can configure the application using environment variables:
# Set the port
PORT=8080 node server.js-
Change the port: Modify the
PORTvariable inserver.jsor set it via environment variables. -
Customize the UI: Edit the CSS in
public/css/chat.cssor modify the Bootstrap components inindex.html. -
Add new features: Extend the Socket.io event handlers in
server.jsto add new functionality.
We welcome contributions from the community! Here's how you can contribute:
- Fork the repository and create your branch from
main. - Write tests for your new features.
- Commit your changes with descriptive messages.
- Open a pull request explaining your changes.
To set up the development environment:
- Clone the repository
- Install dependencies:
npm install
- Start the development server:
npm run dev
- Follow the existing code style
- Use consistent indentation (2 spaces)
- Write clear, concise comments
- Ensure all new code is tested
This project is licensed under the MIT License - see the LICENSE file for details.
Maintainer: Aadhar Gaur
Contributors:
- Your Name - Your Contribution
- Another Contributor - Another Contribution
If you encounter any problems or have feature requests, please open an issue on GitHub. Include:
- Detailed description of the issue
- Steps to reproduce
- Expected behavior
- Screenshots or code snippets if applicable
For questions or support, you can:
- Open an issue on GitHub
- Join our community discussion (link to be added)
- Contact the maintainer directly
Here's what we have planned for future updates:
- Feature 1: Add user authentication
- Feature 2: Implement private messaging
- Feature 3: Add message persistence
- Feature 4: Implement user presence indicators
- Feature 5: Add support for file sharing
If you found this project helpful, please consider giving it a star β on GitHub. Sharing is caring!
This README.md provides a comprehensive guide for developers, making it easy to understand, install, and contribute to the project. It follows modern GitHub README best practices with clear sections, engaging content, and practical examples. The emojis and visual elements make it more appealing, while the detailed structure ensures all necessary information is easily accessible.