Realtime chat platform built with PHP, MySQL, and Ratchet WebSockets. The project demonstrates how to combine a traditional PHP stack with websocket-driven messaging for multi-user chat rooms, online/offline presence, and private conversations, while handling user onboarding via email verification.
- Websocket-powered group chat with message persistence in MySQL.
- One-to-one private messaging with read/unread status tracking.
- Online/offline indicators backed by connection tokens.
- Email-based account verification via PHPMailer.
- Auto-generated avatar images and user profile management.
- Bootstrap 4 UI with client-side validation via Parsley.js.
- PHP 7.4+ with PDO and GD extensions
- MySQL 5.7+ (or compatible)
- cboden/ratchet for the websocket server
- PHPMailer for transactional mail
- Bootstrap, jQuery, Font Awesome, Parsley.js for the front end
bin/ Websocket server bootstrap
database/ Data-access classes and DB connector
images/ Generated avatars (must be writable)
src/ Ratchet message handler (MyApp\Chat)
vendor/ Composer dependencies
vendor-front/ Front-end libraries bundled with the repo
*.php HTTP entry points (auth, chat UI, profile, verification)
- PHP CLI with the
pdo_mysql,sockets, andgdextensions enabled. - Composer for dependency management.
- A running MySQL instance accessible to the PHP process.
- SMTP credentials (or a local mail catcher) for sending verification emails.
- Install PHP dependencies:
composer install
- Create a MySQL database (default name is
chat) and a user with appropriate privileges. - Update database credentials in
database/Database_connection.php. - Ensure the
images/directory is writable by the PHP process (required for avatar generation).
Create the following tables (adjust types/indexes to match your environment):
CREATE TABLE chat_user_table (
user_id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(255) NOT NULL,
user_email VARCHAR(255) NOT NULL UNIQUE,
user_password VARCHAR(255) NOT NULL,
user_profile VARCHAR(255) NOT NULL,
user_status ENUM('Enable','Disabled') DEFAULT 'Disabled',
user_created_on DATETIME NOT NULL,
user_verification_code VARCHAR(255),
user_login_status ENUM('Login','Logout') DEFAULT 'Logout',
user_token VARCHAR(255),
user_connection_id INT
);
CREATE TABLE chatrooms (
id INT AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
msg TEXT NOT NULL,
created_on DATETIME NOT NULL,
INDEX (userid),
CONSTRAINT fk_chatrooms_user FOREIGN KEY (userid) REFERENCES chat_user_table(user_id)
ON DELETE CASCADE
);
CREATE TABLE chat_message (
chat_message_id INT AUTO_INCREMENT PRIMARY KEY,
to_user_id INT NOT NULL,
from_user_id INT NOT NULL,
chat_message TEXT NOT NULL,
timestamp DATETIME NOT NULL,
status ENUM('Yes','No') DEFAULT 'No',
INDEX (to_user_id),
INDEX (from_user_id),
CONSTRAINT fk_chatmessage_to FOREIGN KEY (to_user_id) REFERENCES chat_user_table(user_id)
ON DELETE CASCADE,
CONSTRAINT fk_chatmessage_from FOREIGN KEY (from_user_id) REFERENCES chat_user_table(user_id)
ON DELETE CASCADE
);Update the SMTP placeholders in register.php to match your mail provider. During development you can point these settings to a local mail catcher (e.g., MailHog or Mailpit).
- Start the websocket server (default port 8080):
php bin/server.php
- Serve the HTTP front end (Apache/Nginx or PHP built-in server):
php -S localhost:8000
- Visit
http://localhost:8000/index.phpto register, verify your account, and join the chat.
Note: The browser connects to
ws://localhost:8080. If you change the websocket host/port, update the URL inchatroom.php.
- Use a process manager (Supervisor, systemd, etc.) to keep the Ratchet server running.
- Terminate websocket traffic over TLS (e.g., via Nginx reverse proxy) and adjust the client URL to
wss://. - Hash user passwords before storing them in production (the current code uses plain text for instructional purposes).
- Replace hard-coded email copy and URLs with environment-aware values.
php bin/server.phpfails: ensure thesocketsextension is enabled and port 8080 is free.- Avatars are not generated: confirm the
gdextension is enabled andimages/is writable. - Emails do not send: double-check SMTP credentials and firewall rules; for local testing use a mail catcher.
- Messages are not delivered: verify that both the websocket server and the HTTP server are running, and that the browser can reach
ws://localhost:8080.