Real-time messaging platform utilizing WebSocket for bidirectional communication between clients and a ZeroMQ-powered backend for scalable, asynchronous message distribution. This project enables efficient and low-latency communication across multiple connected clients, with ZeroMQ handling high-throughput messaging and WebSocket ensuring real-time updates in a responsive web interface.
WebSocket Server: Listens for WebSocket connections and broadcasts messages to connected clients.
Clone the repository to your local machine:
git clone https://github.com/kosar501/PhpWebSocket.git
cd your-project-folder
composer install
composer install
There are two main components in the system: the WebSocket server and the Redis queue consumer. You can run both components as separate processes, and they will be managed using Supervisor.
php server.php
[program:websocket-server]
command=php /path/to/your/project/server.php
autostart=true
autorestart=true
stderr_logfile=/var/log/websocket_server.err.log
stdout_logfile=/var/log/websocket_server.out.log
After adding this configuration, update Supervisor:
supervisorctl start websocket-server
Start the WebSocket server:
supervisorctl start websocket-server
If you encounter issues, restart the processes via Supervisor:
supervisorctl restart websocket-server
You can use the Client class to send messages to the WebSocket server through Redis. This class sends messages to the Redis queue that the consumer will process.
$client = new MessagePublisher();
// Prepare the message as an associative array
$message = json_encode(['topic' => 'news', 'content' => 'This is a test message']);
$client->sendMessage($message);
You can check examples folder
const socket = new WebSocket('ws://127.0.0.1:5555');
socket.onmessage = function(event) {
const message = JSON.parse(event.data);
console.log('Received message:', message);
// Now you can access the message properties like message.action, message.username, etc.
};