classes/WebSocket.md

WebSocket

Overview:

WebSocket provides a messaging facade with pluggable drivers for real-time communication. Ships with a Pusher adapter that uses the existing HTTP class (no new dependencies).

Key behavior:

Public API:

Example:

// Configure
WebSocket::using([
    'pusher' => [
        'app_id'  => '12345',
        'key'     => 'your-key',
        'secret'  => 'your-secret',
        'cluster' => 'us2',
    ],
]);

// Send a message
WebSocket::send('notifications', ['type' => 'alert', 'text' => 'New order!']);

// Broadcast
WebSocket::broadcast('chat-room', ['user' => 'Alice', 'message' => 'Hello!']);

// Local event listener
WebSocket::subscribe('orders', function($channel, $data) {
    // Handle locally
});

Adapter interface (WebSocket\Adapter):

interface Adapter {
    public function send($channel, $data);
    public function broadcast($channel, $data);
    public static function valid();
}