OpenSwoole\Http\Response->detach()

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Http\Response->detach(): bool

Parameters

none

Return

bool

If successful, true is returned otherwise false

Description

Detach the HTTP response object with current context from the server. Then you can construct and send back the response within another context such as another Task Worker or make use of some additional TCP server methods which are not available when the response object is setup with a HTTP server context.

By detaching the response object, it is separating the object from the server and means it won't be destroyed when the current Request context finishes, the server won't automatically call $response->end when you have detached the response object. This gives you the opportunity to have more control over the response content and logic.

In most cases you will end up passing the response somewhere else and then using OpenSwoole\Http\Response::create(int $fd) to recreate the response and work with it that way.

Example

There are a few ways to use a detached response in OpenSwoole, let's go through some examples...

Respond from a Task Worker

By detaching the response object, it can then be passed to a task worker process which in the end can send a response instead of the HTTP server request context. This is possible because a detached response object is independent, it is up to you to organise a response. This can be known as a cross-process response.

<?php

$server = new OpenSwoole\Http\Server("127.0.0.1", 9501);

$server->set(['task_worker_num' => 1, 'worker_num' => 1]);

$server->on('Request', function(OpenSwoole\Http\Request $request, OpenSwoole\Http\Response $response) use ($server)
{
    // Detach the response, making it independent and send the file descriptor of the client to a task
    $response->detach();
    $server->task(strval($response->fd));
});

$server->on('Finish', function()
{
    echo "task finish";
});

$server->on('Task', function($server, $taskId, $workerId, $data)
{
    // Recreate the response using the client file descriptor
    $response = OpenSwoole\Http\Response::create($data);

    // Send back a response and show that the task worker is still working asynchronously
    $response->end("Response sent from task worker: #$workerId");
    echo "async task operating...\n";
});

$server->start();

Custom Data Response

When you run a OpenSwoole HTTP server, it handles a lot of the server processing for you like connecting handling, formatting request and response data etc. But you may want more control over how you send back data to a client. You can detach a response and send back raw contents just like you would with a TCP OpenSwoole server. This is known as self-assembly of the HTTP protocol, a more advanced way of sending data to the client if you need more control.

<?php

$server = new OpenSwoole\Http\Server("127.0.0.1", 9502);

$server->on('Request', function(OpenSwoole\Http\Request $request, OpenSwoole\Http\Response $response) use ($server)
{
    // Separate the $response object
    $response->detach();

    // Using the server, send back a HTTP self-assembly response using the connection $fd
    $server->send($response->fd, "HTTP/1.1 200 OK\r\nServer: OpenSwoole\r\n\r\nHello World!\n");
});

$server->start();
Last updated on September 1, 2022