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

namespace IMATHUZH\Messenger\Hooks;

use IMATHUZH\Messenger\Nchan\NchanRequest;

require_once 'common.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' => $channelData
]);
