Remote server timeout when creating webhooks

Hey there @Phil_Seeman, @AndrewWong, thank you so much for your answer, I have found my solution.

The problem was that :
→ I was sending the request to Asana via the createWebhook() method,
→ I was receiving the X-Hook-Secret
→ I was unable to send it back as Php cannot manage asynchronous behavior on it own.

So what I did is I simply used messenger to send the request asynchronously. The Messenger Component (Symfony Docs)
Messenger: Sync & Queued Message Handling (Symfony Docs)

I’ll show my code to help other people encountering that same problem in the future

In my case, I only want to receive a notification when a task is being completed or uncompleted

So right below is simply my createWebhook() method in my AsanaService()

public function createWebhook(string $taskId)
    {
        $client = $this->client();

        $webhook = $client->webhooks->createWebhook([
            "filters" => [
                [
                    "action" => "changed",
                    "fields" => [
                        "completed"
                    ],
                    "resource_type" => "task",
                ]
            ],
            "resource" => $taskId,
            "target" => "https://TARGET.URL/webhook/<WebhookToken>/test-asana"
        ]);

        return $webhook;
    }

Again right below is my webhook controller

/**
     * @Route("/test-asana", name="test_asana_webhook", methods={"POST"})
     */
    public function confirmAsanaWebhook(Request $request)
    {

        // Here I will be searching if the incoming request has an empty body or not via $request->getContent()
        // If the request has an empty body (empty array) it means the incoming request is actually the webhook confirmation via the X-Hook-Secret
        // So I will simply return the X-Hook-Secret with a 200 status Code

        return new JsonResponse('', Response::HTTP_OK, [
            'X-Hook-Secret' => $request->headers->get('X-Hook-Secret'),
        ], true);

        // Now if the request sends me an event body, I will do my work (retrieve the task, do my process, etc)
        
    }

Regarding messenger I create my class

<?php

namespace App\Message;

class AsanaWebhookCreation
{
    /**
     * @var string
     */
    private $taskId;

    public function __construct(string $taskId)
    {
        $this->taskId = $taskId;
    }

    /**
     * @return string
     */
    public function getTaskId(): string
    {
        return $this->taskId;
    }
}

I create the MessageHandler

<?php

namespace App\MessageHandler;

use App\Message\AsanaWebhookCreation;
use App\Service\Asana\AsanaService;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Notifier\NotifierInterface;

class AsanaWebhookCreationHandler implements MessageHandlerInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * @var NotifierInterface
     */
    private $notifier;
    /**
     * @var MessageBusInterface
     */
    private $messageBus;
    /**
     * @var AsanaService
     */
    private $asanaService;

    public function __construct(NotifierInterface $notifier, MessageBusInterface $messageBus, AsanaService $asanaService)
    {
        $this->notifier = $notifier;
        $this->messageBus = $messageBus;
        $this->asanaService = $asanaService;
    }

    public function __invoke(AsanaWebhookCreation $asanaWebhookCreation)
    {
        $this->asanaService->createWebhook($asanaWebhookCreation->getTaskId());
    }
}

then in any of my controller route or anywhere really I’ll just dispatch the message

 /**
     * @Route("/", name="index")
     */
    public function index(Request $request, MessageBusInterface $messageBus): Response
    {

        $messageBus->dispatch(new AsanaWebhookCreation('1201859121096487')); // parameter = task gid


return $this->render('index.html.twig', []);
    }
1 Like

@anon14122699,

Excellent - glad you got it working, and especially thanks for posting your working code for others going forward!