Skip to content

Latest commit

 

History

History
137 lines (113 loc) · 12.2 KB

File metadata and controls

137 lines (113 loc) · 12.2 KB

Building Fast APIs and Middlewares: Mezzio Swoole

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Speaker Introduction and Background

The speaker, Barar Dei from Nigeria, shares his experience as a senior software architect at ABA and King Systems and founder of Inability. He leads a team building end-to-end software for digital insurance companies, focusing on seamless, low-bug products. This is his second talk to PHP developers, covering Mezzio and Swoole for fast APIs.

  • Key Takeaway: Contributions to projects like Oku Shad for Kubernetes highlight practical expertise in building systems that ease daily life.
  • Link for More Details: Ask AI: Speaker Background

Why APIs Need to Be Fast

In today's digital world, users demand quick responses—attention spans are short, and modern frontend frameworks like Angular, Vue, and React push for speed. Fast APIs serve impatient customers, supporting both private frontends and public external clients, plus long-running jobs.

  • Key Takeaway: Mezzio and Swoole combo powers private/public APIs and tasks, ensuring efficiency without waiting for processes like loading spinners.
  • Link for More Details: Ask AI: Importance of Fast APIs

Introduction to Mezzio

Mezzio, evolved from Zend Expressive, builds PSR-15 middleware applications with layered architecture. It handles PSR-7 messages, supports routers like FastRoute, Aura Router, and Laminas Router, and enables dependency injection via PSR-11 (e.g., Laminas Service Manager). Templating options include Twig, Plates, and Laminas View, with error handling via Whoops.

  • Key Takeaway: Focus on API-building without deep templating dives, but it's great for structured, middleware-based projects.
  • Link for More Details: Ask AI: Mezzio Overview

Understanding Middlewares in Mezzio

Middlewares sit between requests and responses, processing incoming requests, taking actions, and either returning responses or delegating to the next layer. They follow PSR-15 interfaces: Handle method for requests/responses, Process for delegation. This enables neat workflows like authentication followed by access control.

  • Key Takeaway: Benefits include composable building blocks, lightweight organization, reusable code across endpoints, and interoperability. Pipelines route requests, dispatching matches or falling to 404 handlers.
// Sample PSR-15 RequestHandlerInterface
public function handle(ServerRequestInterface $request): ResponseInterface;

Code Examples with Mezzio Middlewares

Routes define HTTP methods (POST, GET, etc.) with reusable middlewares. For instance, a "find user by email" middleware checks databases, adds attributes to requests for next layers (e.g., create user if not found, or check password for login). Callable middlewares allow inline functions matching PSR-15 signatures.

  • Key Takeaway/Example: In signup, if user exists (from attribute), throw exception; else create. In login, if not found, throw exception. This reuse keeps code DRY.
// Sample route with reusable middleware
$app->post('/api/users/signup', [
    FindUserByEmail::class,
    CreateUser::class,
]);

Getting Started and Additional Mezzio Features

Start with composer create-project laminas/laminas-mezzio-skeleton. Additional middlewares cover problem details for structured errors, OAuth2 for authorization.

Introduction to Swoole

Swoole is an event-driven, async, coroutine-based concurrency library for high-performance PHP. It creates servers for HTTP, WebSocket, TCP, UDP. Unlike traditional PHP (e.g., Apache), it loads apps once in memory, avoiding repeated bootstrapping for faster requests.

  • Key Takeaway: Directions include Swoole and OpenSwoole; choose based on needs.
  • Link for More Details: Ask AI: Swoole Basics

Swoole Architecture and Features

Manager process bootstraps the app in memory, forking to event/web workers for requests (isolated, one request per worker). Task workers handle long-running jobs. Scale workers for traffic. Supports Docker, Xdebug (from v5.0).

  • Key Takeaway: Persistent memory state speeds up subsequent requests compared to traditional PHP.
  • Link for More Details: Ask AI: Swoole Architecture

Coroutines and Concurrency in Swoole

Coroutines enable concurrent execution (e.g., 10,000 MySQL queries in 0.2s). Channels share data between coroutines; wait groups/barriers sync multiple ones. Connection pools for MySQL/PostgreSQL/Redis auto-reconnect on timeouts.

  • Key Takeaway/Example: Run two calls concurrently and join results.
// Sample coroutine
go(function () { /* task1 */ });
go(function () { /* task2 */ });

Combining Mezzio and Swoole

Install via Composer/PECL, configure for coroutines, workers, host/port. Enable task coroutines. Start server to bootstrap Mezzio in memory.

Practical Examples: Async Tasks and Cron Jobs

Use coroutines for async emails (e.g., OTP via SendGrid) without blocking responses. Delegate long-running tasks (e.g., Azure image recognition) to task workers via delegators. For cron jobs, use timers/event loops to schedule and dispatch events/classes.

  • Key Takeaway/Example: In middleware, Co\run(function() { sendOtp(); }); returns response fast, email sends async. For tasks: $server->task($model);.
// Sample task delegator
$server->on('task', function ($server, $taskId, $reactorId, $data) { /* process */ });

Benchmarks, Considerations, and Challenges

Benchmarks show Swoole outperforming regular PHP, NGINX combos (links provided). Considerations: Scale workers for load, keep services stateless (avoid sessions), use factories for lazy instantiation (e.g., Azure blobs). Wrap Doctrine connections in delegators to handle timeouts.

Resources and Q&A

Resources: Mezzio docs, Swoole English docs, Doctrine integration. Q&A covers: Manager recreates crashed workers on 500s; coroutines are C-extension based (not fibers); Xdebug essential for Swoole dev, restart server on code changes (or use hot reload).


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: