The Chat Application is a real-time chat system built using Node.js, Express, and Socket.io. It allows users to join different chat rooms, send messages, and share their locations.
- Express
- Socket.io
- Bad-Words
The project consists of two main components:
-
Server-Side (index.js):
- Handles WebSocket connections using Socket.io.
- Manages user interactions, messages, and room data.
-
Utilities (utils/users.js):
- Contains functions for user management, including adding, removing, and retrieving users.
Clone the repository and install the required dependencies:
git clone <repository_url>
cd <project_directory>
npm install
Start the server:
npm start
By default, the server runs on http://localhost:3000
. You can customize the port using the PORT
environment variable.
The application uses the following socket events:
- connection: Handles new WebSocket connections.
- join: Allows users to join a specific chat room.
- sendMessage: Sends text messages to the chat room.
- sendLocation: Shares the user's location.
- disconnect: Handles user disconnections.
The application generates and broadcasts various messages, including welcome messages, user join notifications, user leave notifications, and user chat messages.
Users can share their location, which is then transformed into a Google Maps link.
The addUser
function in utils/users.js
adds users to the system. It performs data validation and ensures unique usernames within a room.
The removeUser
function in utils/users.js
removes users based on their socket ID.
- getUser: Retrieves a user based on their socket ID.
- getUsersInRoom: Retrieves all users in a specific chat room.
- The project uses Express for the web server and Socket.io for real-time communication.
- Bad-Words is used to filter out profanity from user messages.
The utils/messages.js
module provides utility functions for generating different types of messages used in the chat application.
Generates a standard text message object with the following properties:
username
: The sender's username.text
: The message content.createdAt
: The timestamp when the message is created.
Example:
const message = generateMessage('JohnDoe', 'Hello, chat world!');
console.log(message);
Output:
{
"username": "JohnDoe",
"text": "Hello, chat world!",
"createdAt": 1646304000000
}
The Join Page serves as the entry point for users to join the chat application. It contains a form where users can input their display name and the desired chat room.
- Structure:
<h1>
: Displays the "Join" heading.- Form:
- Input for the display name (
username
). - Input for the chat room (
room
). - "Join" button to submit the form.
- Input for the display name (
The Chat Page is the main interface for users actively engaged in the chat. It consists of two main sections: the chat sidebar and the chat main area.
-
Structure:
- Chat Container:
#sidebar
: A container for the chat sidebar, which displays room information and the list of users.#messages
: The chat messages area where messages are dynamically displayed.- Compose Section:
- Form (
#messageForm
): Allows users to input and send text messages. - Input field for typing messages (
message
). - "Send" button to submit messages.
- "Send Location" button to share location.
- Form (
- Message Templates:
<script id="messageTemplate" type="text/html">
: Template for displaying text messages.<script id="locationMessageTemplate" type="text/html">
: Template for displaying location messages.<script id="sidebarTemplate" type="text/html">
: Template for displaying room information and the list of users.
- Chat Container:
-
External Libraries:
- Mustache.js: Used for templating.
- Moment.js: Library for handling timestamps.
- qs.js: Helps with parsing query strings.
- Socket.io.js: The client-side Socket.io library for real-time communication.
-
Application Script:
./js/chat.js
: The application script responsible for handling client-side logic, interacting with the server, and updating the UI based on received messages.
The chat.js
file handles the client-side logic for the chat application. It utilizes Socket.io for real-time communication and Mustache.js for rendering message templates.
const socket = io(); // Initializes Socket.io connection.
// Elements
const $messageForm = document.querySelector('#messageForm');
const $messageFormInput = document.querySelector('input');
const $messageFormButton = document.querySelector('button');
const $userLocation = document.querySelector('#sendLocation');
const $messages = document.querySelector('#messages');
const $sidebar = document.querySelector('#sidebar');
These variables represent key elements on the chat page, facilitating interactions and updates.
// Templates
const messageTemplates = document.querySelector('#messageTemplate').innerHTML;
const locationMessageTemplate = document.querySelector(
'#locationMessageTemplate'
).innerHTML;
const sidebarTemplate = document.querySelector('#sidebarTemplate').innerHTML;
These variables store Mustache.js templates for rendering different types of messages and the sidebar.
// Options
const { username, room } = Qs.parse(location.search, {
ignoreQueryPrefix: true,
});
The Qs
library parses the query parameters from the URL, extracting the username
and room
values.
const autoScroll = () => {
// Function to auto-scroll the chat messages to the latest.
};
This function ensures that the chat messages automatically scroll to the latest message as new messages are added.
-
message
Event:- Renders and displays text messages in the chat area.
-
locationMessage
Event:- Renders and displays location messages in the chat area.
-
sendMessage
Event:- Handles form submission to send text messages to the server.
-
sendLocation
Event:- Handles button click to share the user's location.
-
join
Event:- Sends information about the user joining the chat room.
-
roomData
Event:- Renders and updates the sidebar with room information and user list.
-
emit("sendMessage", message, callback)
Event:- Emits the
sendMessage
event to the server with the user's message.
- Emits the
-
emit("sendLocation", { latitude, longitude }, callback)
Event:- Emits the
sendLocation
event to the server with the user's location.
- Emits the
-
emit("join", { username, room }, callback)
Event:- Emits the
join
event to the server when a user joins the chat room.
- Emits the
The file manages UI updates by rendering messages and updating the sidebar when specific Socket.io events occur.