Flake Documentation

Welcome to the Flake documentation. This guide will help you get started with building high-performance PHP servers using the Flake framework.

Getting Started

Flake is a PHP server/daemon framework that supports both forking and event-driven patterns. It provides a simple yet powerful API for building network servers.

Installation

git clone https://github.com/animeshkundu/flake.git

Requirements

  • PHP 7.4 or higher
  • ext-sockets extension
  • ext-pcntl extension (for forking mode)
  • libevent extension (optional, for event-driven mode)

Quick Start

Here's a simple example of creating a TCP server:

<?php
require_once 'flake.php';

class MyHandler extends \Flake\Handler {
    public function Handle_Read($data) {
        $this->Write("Hello from Flake!\n");
    }
}

$core = new \Flake\Core();
$core->Listen('tcp://0.0.0.0:8080', 'MyHandler');
$core->Run();

Core Concepts

Socket

The base class for all socket operations. Handles read/write operations, TLS/SSL support, and buffer management.

Listener

Listens for incoming connections on a specified address and port. Creates new Handler instances for each connection.

Handler

Base class for protocol handlers. Extend this class to implement your own protocol logic.

Core

The main event loop. Manages all listeners and handlers, and dispatches events.

Server Patterns

Forking Mode

Each connection is handled by a separate forked process. This provides isolation and safety - if one handler crashes, others are unaffected.

  • Supports ~483 concurrent connections
  • Process isolation for safety
  • Watch for thundering herd problem

Event-Driven Mode

Uses libevent for high-performance I/O multiplexing. All connections are handled in a single process.

  • Supports 1000+ concurrent connections
  • Solves the c10k problem
  • Be careful with blocking operations