This project implements a real-time notification system for a social app using PHP, MySQL, jQuery, and WebSockets (via Ratchet). It also includes a long polling fallback for environments where WebSockets are not supported.
- PHP 7.4 or higher
- MySQL 5.7 or higher
- Composer (for installing Ratchet)
- A web server (e.g., Apache or Nginx)
- Node.js (optional, for WebSocket testing)
- Port 8080 open for WebSocket server
-
Clone or Download the Project Download the project files or clone the repository to your local machine.
-
Install Dependencies Install Ratchet for WebSocket support using Composer:
composer require cboden/ratchet
-
Set Up the Database
- Create a MySQL database named
social_app
. - Execute the
schema.sql
file to create thenotifications
table:mysql -u your_username -p social_app < schema.sql
- Update the database credentials (
your_username
,your_password
,social_app
) innotification_system.php
andfetch_notifications.php
.
- Create a MySQL database named
-
Configure the Web Server
- Place
index.html
andfetch_notifications.php
in your web server's root directory (e.g.,/var/www/html
for Apache). - Ensure PHP is enabled on your web server.
- Place
-
Run the WebSocket Server Start the WebSocket server by running:
php notification_system.php
This will start the server on
ws://localhost:8080
. Ensure port 8080 is open. Bettwe use IP instead of localhost for testing in LAN.
-
Access the Application
- Open
index.html
in a web browser (e.g.,http://localhost/index.html
). - Enter a
User ID
andMessage
in the input fields. - Click Send Notification to send a notification.
- Open
-
Real-Time Notifications
- Notifications are sent via WebSocket and displayed in real-time to all connected clients.
- If WebSocket fails, the system switches to long polling, fetching notifications from
fetch_notifications.php
every few seconds.
-
Database Storage
- Notifications are stored in the
notifications
table in the MySQL database for persistence.
- Notifications are stored in the
notification_system.php
: WebSocket server using Ratchet to handle real-time notifications.index.html
: Frontend interface using jQuery for sending and displaying notifications.fetch_notifications.php
: Backend script for long polling fallback to fetch notifications.schema.sql
: SQL script to create thenotifications
table.
- Replace
your_username
,your_password
, andsocial_app
in the PHP files with your actual MySQL credentials and database name. - For production, secure the WebSocket connection with WSS (WebSocket Secure) and implement user authentication.
- If WebSockets are not feasible, you can rely solely on long polling by removing the WebSocket code and using only
fetch_notifications.php
. - Ensure your web server and WebSocket server are running simultaneously.
- WebSocket Connection Fails: Check if port 8080 is open and the WebSocket server (
notification_system.php
) is running. - Database Errors: Verify MySQL credentials and ensure the
notifications
table exists. - Notifications Not Displaying: Ensure jQuery is loaded and check the browser console for errors.
This project is licensed under the MIT License.