Skip to main content

Command Palette

Search for a command to run...

Asynchronous Tasks Made Easy with Laravel Terminable Middleware

From Sync to Async: How Terminable Middleware Can Transform Your Laravel App

Published
2 min read
Asynchronous Tasks Made Easy with Laravel Terminable Middleware
S

4+ years of experience building scalable backend systems using Node.js, Laravel, and Go. Moving towards Solution Architecture with strong foundations in distributed systems and cloud.

Passionate about system design, databases, protocols, and high-performance services.

Laravel Stream-Pulse Laravel package for Event-Driven Architecture (EDA) using Redis Streams.

Enables scalable event publishing & consuming directly inside Laravel. Ideal for real-time apps and microservice event workflows. GoQueue Lightweight, production-ready job queue in Go with multiple backends (Redis, SQLite, SQS).

Supports retries, dead-letter queues, and graceful shutdown. Benchmarked for high-throughput event processing.

Introduction

Hi Folks, as developers, we often encounter situations where we need to perform tasks that take a significant amount of time, such as scraping data from social media or indexing large datasets. However, we don't want to keep our users waiting for the task to complete. In this article, we'll explore how to use Laravel's terminable middleware to handle asynchronous tasks without using queues or other complex solutions.

The Problem

We have two applications: Application 1 (user-facing) and Application 2 (logic for scraping and indexing data). Application 1 initiates an API call to Application 2, but we don't want to wait for the task to complete before sending a response. We need a way to handle this asynchronously without using queues or other complex solutions.

The Solution

Enter Laravel's terminable middleware. Terminable middleware is a type of middleware that runs after the response has been sent to the user. This makes it perfect for handling asynchronous tasks that don't require an immediate response.

Example Code

// app/Http/Middleware/ScrapeData.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class ScrapeData
{
    public function handle(Request $request, Closure $next)
    {
        // Perform the API call to Application 2
        $response = $next($request);

        // Return the response immediately
        return $response;
    }

    public function terminate($request, $response)
    {
        // Scrape data from social media and update the database
        // This code will run after the response has been sent to the user
        //...
    }
}

In this example, the handle method performs the API call to Application 2 and returns the response immediately. The terminate method is called after the response has been sent to the user and is where you can perform the asynchronous task of scraping data from social media and updating the database.

Conclusion

Laravel's terminable middleware provides a simple and elegant solution for handling asynchronous tasks without using queues or other complex solutions. By using terminable middleware, you can improve the performance of your application and provide a better user experience.

Try using terminable middleware in your next Laravel project to handle asynchronous tasks. Share your experiences and tips in the comments below!