Course contentsShow
← Node.js
Lesson 1299 of 1,98011. Task Queues and Background ProcessingFree lesson

Coordinating Background Work with Webhooks

Trigger external system updates once a long-running background task completes.

In this lesson, you will learn how to notify external systems of your background task results by triggering webhooks once your worker completes its job.

The Role of Outbound Webhooks

When you offload heavy computation—like generating a complex PDF report or processing a video—to a background worker, your main application eventually loses track of that task’s progress. To keep your frontend or a third-party partner informed, you shouldn't rely on polling the database. Instead, use webhooks to "push" the outcome to the destination as soon as the worker finishes.

Implementing the Callback Pattern

When you enqueue a background job, your payload should include a webhookUrl. Once the worker completes its logic, it performs an HTTP POST request to that URL.

Here is how you might structure a simple task processor to handle this:

const axios = require('axios');

async function processDataJob(job) {
  try {
    const result = await performHeavyComputation(job.data);
    
    // Once task is done, notify the external system
    if (job.data.webhookUrl) {
      await axios.post(job.data.webhookUrl, {
        status: 'success',
        jobId: job.id,
        result: result
      });
    }
  } catch (error) {
    // Handle error webhook here
    await axios.post(job.data.webhookUrl, { status: 'failed', error: error.message });
  }
}

Critical Considerations

When coordinating work via webhooks, remember three rules:

  1. Idempotency: The receiving system might get the same webhook twice due to network retries. Ensure the receiver can handle duplicate requests gracefully.
  2. Timeouts: Don’t let a slow webhook endpoint hold up your worker. Wrap your webhook call in a short timeout or offload the notification to a separate "notification queue" so the worker can move on to the next job immediately.
  3. Security: Always sign your webhook payloads using a secret key (HMAC) so the receiver knows the request genuinely came from your system.

By treating the webhook as a final step in your job lifecycle, you decouple your background processes from your communication layer, allowing your workers to remain focused solely on execution.

Key Takeaway: Use webhooks as a push-based notification mechanism to alert external systems of job completion, ensuring your architecture remains decoupled and event-driven.