<?php declare(strict_types=1);
/**
 * @package messenger
 * @author kputyr
 * @date 23.03.2026
 * 
 * This script runs when a client connects
 */

namespace IMATHUZH\Messenger;

require_once 'request.php';

$request = NchanRequest::fromServerRequest()->generateUrls();

// Close the request, so that the client can connect and receive the hello message
$request->close(200);
$request->notifyController('sub');

// Prepend dedicated user and session channels to listener channels
$channelData = $request->getChannelMetaData();
array_unshift(
    $channelData['sub'],
    "user." . $request->getClientId(),
    "ses." . $request->getSessionId()
);
// Send channel data to the client
$request->respond([
    'type' => 'ctl',
    'name' => 'hello',
    'payload' => $request->getChannelMetaData()
]);
