Ratchet WebSocket Documentation
Ratchet is a PHP library that enables web developers to easily create real-time, bi-directional applications between clients and servers over WebSockets. By simplifying the complexity of WebSocket connections, Ratchet provides a straightforward interface for messaging, event handling, and connection management. This makes it ideal for building interactive applications such as live chat systems, real-time data dashboards, and collaborative tools.
Adding to the development experience, Prisma PHP enhances the usability of Ratchet by automating server creation and introducing an auto-refresh feature that detects changes in files. This seamless integration significantly improves the development experience, allowing developers to focus more on building features rather than dealing with the setup and manual refresh processes. Prisma PHP's approach ensures that developers have a more efficient and enjoyable development workflow, making it easier to incorporate real-time functionalities into PHP applications. Together, Ratchet and Prisma PHP provide a powerful combination for developers looking to offer the best real-time features in their web applications, backed by an excellent development experience.
For more information Ratchet WebSockets for PHP
WebSocket Server Setup
NOTE: To use this feature you have to chose websocket while you creating the project
Details the setup process for the WebSocket server using Ratchet's IoServer, HttpServer, and WsServer components, along with the custom ConnectionManager
.
Location in Prisma PHP ./src/lib/websocket/server.php
require __DIR__ . '/../../../vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Lib\Websocket\ConnectionManager;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new ConnectionManager()
)
),
8080
);
$server->run();
WebSocket Connection Management
Explains the role of the ConnectionManager
class in handling connections, messaging, and errors within the WebSocket server environment.
Location in Prisma PHP ./src/lib/websocket/ConnectionManager.php
namespace Lib\Websocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class ConnectionManager implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
// Connection open code
}
public function onMessage(ConnectionInterface $from, $msg) {
// Message handling code
}
public function onClose(ConnectionInterface $conn) {
// Connection close code
}
public function onError(ConnectionInterface $conn, \Exception $e) {
// Error handling code
}
}
WebSocket Frontend HTML and Javascript
The JavaScript required for establishing the WebSocket connection, sending messages, and displaying incoming messages dynamically:
Location in Prisma PHP ./src/app/custom-rout/index.php
Confirm the existence of const ws = new WebSocket("ws://localhost:8080");
in the JavaScript code. ./src/app/js/index.js
const ws = new WebSocket("ws://localhost:8080"); // NOTE: If you chose using websocket while creating the project, this is not needed
ws.onopen = function(event) {
console.log("Connected to the WebSocket server");
};
ws.onmessage = function(event) {
// Handling incoming messages
};
document.getElementById('messageForm').addEventListener('submit', function(event) {
event.preventDefault();
// Form submission logic
});