PHP code example of octo-php / symfony-realtime

1. Go to this page and download the library: Download octo-php/symfony-realtime library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

octo-php / symfony-realtime example snippets


use Octo\RuntimePack\ServerBootstrap;
use Octo\SymfonyRealtime\RealtimeServerAdapter;

$adapter = new RealtimeServerAdapter(
    httpHandler: $httpKernelAdapter,
    wsHandler: $myWebSocketHandler,
);

ServerBootstrap::run(
    appHandler: $adapter,
    production: true,
);

use Octo\SymfonyRealtime\WebSocketHandler;
use Octo\SymfonyRealtime\WebSocketContext;

final class ChatHandler implements WebSocketHandler
{
    public function onOpen(WebSocketContext $context): void
    {
        // Connexion ouverte
    }

    public function onMessage(WebSocketContext $context, string $data): void
    {
        // Frame reçue — répondre directement
        $context->send('Echo: ' . $data);
    }

    public function onClose(WebSocketContext $context): void
    {
        // Connexion fermée
    }
}

use Octo\SymfonyRealtime\SseEvent;

$event = new SseEvent(
    data: "Hello world",
    event: 'message',
    id: '42',
    retry: 3000,
);

echo $event->format();
// event: message
// data: Hello world
// id: 42
// retry: 3000
//
bash
composer