logo
podcast Podcast
get help Get Unstuck

Simplifying webhook client responses

Especially useful with high volume projects

Joel Clermont
Joel Clermont
2024-07-22

I really like the Laravel webhook client package (from Spatie, of course).

On a recent project, I was implementing it to receive a potentially high volume of webhooks, so I was thinking a bit more about performance.

One admittedly "micro" optimization I identified was that we didn't need to start a session or return a cookie when returning a response to an incoming webhook.

In order to avoid this, you can exclude three middleware from the webhook receiving route:

  • StartSession
  • ShareErrorsFromSession
  • VerifyCsrfToken

Here's what that would look like in your routes file:

Route::webhooks('url-path', 'config-name')
    ->withoutMiddleware([
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ]);

So now the responses are a bit more lightweight. And as a bonus, I no longer need to exclude this webhook route from CSRF verification.

For most projects, this may be overkill, but it's worth considering if you're expecting a lot of inbound webhooks.

Here to help,

Joel

P.S. Are you dealing with performance bottlenecks in your Laravel application? Let's talk!

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.