Create Secure Web Socket (WSS) with Ratchet PHP
Ratchet is a PHP library for building real-time, bidirectional applications using WebSockets. By default it runs over plain WebSocket (ws://), but production environments require the secure variant (wss://) to operate over HTTPS pages and to avoid browser security restrictions.
This guide shows how to set up a secure WebSocket server using Ratchet with SSL/TLS via the ReactPHP SecureServer component.
Requirements
- Ratchet installed via Composer
- ReactPHP socket library (pulled in automatically with Ratchet)
- A valid SSL certificate and private key (Let's Encrypt works fine)
Step 1: Install Ratchet
composer require cboden/ratchet
Step 2: Create Your Socket Application Class
Implement the MessageComponentInterface with the required methods:
<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Socket 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";
}
public function onMessage(ConnectionInterface $from, $msg)
{
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} closed\n";
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "Error: {$e->getMessage()}\n";
$conn->close();
}
}
Step 3: Create the Secure Server Bootstrap
Create a server.php file that wraps your application in a SecureServer with your SSL certificates:
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\SecureServer;
$app = new HttpServer(
new WsServer(
new Socket()
)
);
$loop = Factory::create();
$socket = new Server('0.0.0.0:8080', $loop);
$secureSocket = new SecureServer($socket, $loop, [
'local_cert' => '/etc/letsencrypt/live/yourdomain.com/fullchain.pem',
'local_pk' => '/etc/letsencrypt/live/yourdomain.com/privkey.pem',
'allow_self_signed' => false,
'verify_peer' => false,
]);
$server = new IoServer($app, $secureSocket, $loop);
$server->run();
Replace the certificate paths with the actual paths to your SSL certificate files.
Step 4: Run the Server
php server.php
The server listens on port 8080 over WSS. Clients connect using:
const socket = new WebSocket('wss://yourdomain.com:8080');
Step 5: Verify the Connection
Use the WebSocket echo test at websocket.org/echo.html to confirm the WSS connection is working. Enter your wss:// URL and check that messages echo back correctly.
Notes
- Ensure port 8080 (or whichever port you choose) is open in your server's firewall.
- Let's Encrypt certificates expire every 90 days. Set up automatic renewal with
certbot renewvia cron. - If your server sits behind an Nginx reverse proxy, you can terminate SSL at Nginx and forward plain WebSocket traffic internally, avoiding the need for PHP-level SSL handling.
Conclusion
Adding SSL to a Ratchet WebSocket server requires wrapping the React socket server with SecureServer and providing your certificate paths. The change from ws:// to wss:// is essential for any production deployment, particularly when your web app is served over HTTPS — browsers will block mixed-content WebSocket connections.