This project illustrates how to use WebSocket in Hypertext Processor (PHP) using the Ratchet library. It's a simple chat/messaging app that demonstrates the benefits of WebSocket over traditional polling, where the HTTP protocol remains open, allowing real-time updates without the need for constant refreshing.
Traditional polling involves sending requests to the server at regular intervals to check for updates. This approach has several disadvantages:
- Increased Server Load: Constantly sending requests to the server increases its load and can lead to performance issues.
- Higher Latency: There is a delay between the time when data is updated on the server and when it is received by the client.
- Inefficiency: Many requests return without any new data, leading to unnecessary network traffic.
WebSocket solves these issues by maintaining an open connection between the client and server:
- Real-Time Updates: The server can push updates to the client instantly, reducing latency.
- Reduced Server Load: Fewer requests are sent since the connection remains open, leading to more efficient use of resources.
- Efficient Communication: Data is sent only when there are updates, reducing unnecessary network traffic.
- Send messages instantly to other users.
- Generate profile avatars and usernames, stored in the database.
- Real-time availability of users for chatting.
- Install the Ratchet PHP library for WebSocketing.
- Import the provided database located in the database/ folder.
- Run the WebSocket server script (server.php) found under the async/ folder using the console.
- Clone the repository to your local environment.
- Install Composer dependencies using composer install.
- Import the database from the database/ folder.
- Navigate to the async/ folder and run php server.php in your terminal to start the WebSocket server.
- Open the project in your web browser to start using the chat app.
Ratchet PHP Library - For WebSocket functionality.