{"id":21057793,"url":"https://github.com/driftphp/server","last_synced_at":"2025-04-13T04:11:43.681Z","repository":{"id":35932787,"uuid":"217268909","full_name":"driftphp/server","owner":"driftphp","description":":cyclone: ReactPHP based non-blocking server ","archived":false,"fork":false,"pushed_at":"2023-11-14T15:50:41.000Z","size":262,"stargazers_count":68,"open_issues_count":5,"forks_count":13,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-24T09:21:09.308Z","etag":null,"topics":["driftphp","reactphp","server","socket"],"latest_commit_sha":null,"homepage":"https://driftphp.io","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/driftphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-24T10:05:25.000Z","updated_at":"2024-01-14T12:45:03.000Z","dependencies_parsed_at":"2023-11-14T17:00:00.917Z","dependency_job_id":null,"html_url":"https://github.com/driftphp/server","commit_stats":{"total_commits":89,"total_committers":6,"mean_commits":"14.833333333333334","dds":0.1123595505617978,"last_synced_commit":"65451b9c3c0270b5480c1d504d6f7cf1a1b22734"},"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Fserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Fserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Fserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Fserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/driftphp","download_url":"https://codeload.github.com/driftphp/server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248661705,"owners_count":21141451,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["driftphp","reactphp","server","socket"],"created_at":"2024-11-19T17:04:45.540Z","updated_at":"2025-04-13T04:11:43.651Z","avatar_url":"https://github.com/driftphp.png","language":"PHP","readme":"# DriftPHP Server\n\n[![CircleCI](https://circleci.com/gh/driftphp/server.svg?style=svg)](https://circleci.com/gh/driftphp/server)\n\nThis package provides a ReactPHP based async, reactive and non-blocking server\nfor PHP applications working on top of ReactPHP Promises and PSR standards. The \nserver has a small kernel/application abstraction for an optimized integration\nfor several domain implementations. Here some features.\n\n- Handle request based on Promises\n- Serve static content in a non-blocking way\n- Compress both Responses and Stream data\n- Work with different workers, using multiple PHP threads (and CPUs)\n- Visualize the usage and check how fast and light your request handles are\n- Use the PHP-Watcher (only available if the PHP-Watcher is included in your \n  composer) to automatically update the server if you change some of your code\n\nBy default, the server will use the DriftPHP Kernel adapter, but you can change\nthe adapter easily when starting the server (Check the [adapters](#build-your-adapter) \nchapter)\n\n### Table of content\n\n- [Installation](#installation)\n- [Start the server](#start-the-server)\n- [Build your adapter](#build-your-adapter)\n- [Workers](#workers)\n- [Watcher](#watcher)\n- [Static server](#static-server)\n- [Symfony bridge](#symfony-bridge)\n- [DriftPHP resources](#driftphp-resources)\n\n## Installation\n\nYou can install the server by adding the dependency in your `composer.json`\nfile\n\n```json\n\"require\": {\n    \"drift/server\": \"^0.1\"\n}\n```\n\n## Start the server\n\nThis package provides an async server for DriftPHP framework based on ReactPHP\npackages and Promise implementation. The server is distributed with all the\nSymfony based kernel adapters, and can be easily extended for new Kernel\nmodifications.\n\nTo start the server, just type this line. Your project might have a custom `bin`\nfolder, so check it.\n\n```bash\nphp vendor/bin/server run 0.0.0.0:8000\n```\n\nYou can use as well the short mode, defining only the port and assuming this \nhost `0.0.0.0`.\n\n```bash\nphp vendor/bin/server run 8000\n```\n\nAnd that's it. You will have a fully working server for your application.\n\n## Build your adapter\n\nIn order to build your adapter, the only thing you need is to create an \nimplementation of the interface `Drift\\Server\\Adapter\\KernelAdapter`. This layer\nwill allow the server to start your application, handle each request, locate\nyour static resources and shutdown the application. The `ObservableKernel` will\nprovide as well some information about where your code is located, specifically\ndesigned for the watcher feature.\n\n```php\n/**\n * Class KernelAdapter.\n */\ninterface KernelAdapter extends ObservableKernel\n{\n    /**\n     * @param LoopInterface            $loop\n     * @param string                   $rootPath\n     * @param ServerContext            $serverContext\n     * @param OutputPrinter            $outputPrinter\n     * @param MimeTypeChecker          $mimeTypeChecker\n     * @param FilesystemInterface|null $filesystem\n     *\n     * @return PromiseInterface\u003cself\u003e\n     *\n     * @throws KernelException\n     */\n    public static function create(\n        LoopInterface $loop,\n        string $rootPath,\n        ServerContext $serverContext,\n        OutputPrinter $outputPrinter,\n        MimeTypeChecker $mimeTypeChecker,\n        ?FilesystemInterface $filesystem\n    ): PromiseInterface;\n\n    /**\n     * @param ServerRequestInterface $request\n     *\n     * @return PromiseInterface\u003cResponseInterface\u003e\n     */\n    public function handle(ServerRequestInterface $request): PromiseInterface;\n\n    /**\n     * Get static folder.\n     *\n     * @return string|null\n     */\n    public static function getStaticFolder(): ? string;\n\n    /**\n     * @return PromiseInterface\n     */\n    public function shutDown(): PromiseInterface;\n}\n```\n\nWhen you have your adapter created, the is as easy is this to start serving from\nyour application\n\n```bash\nphp vendor/bin/server run 0.0.0.0:8000 --adapter='My\\Namespace\\Adapter\"\n```\n\n### Custom response output\n\nYou can internally use the `x-server-message` header for custom server messages.\nThe server will remove this server value before returning the response content.\n\n## Workers\n\nThis server creates a single worker by default. A simple PHP thread that will \nuse one single CPUs. Luckily this server provides you a simple way of creating\nmultiple instances listening the same port, emulating a simple balancer between\nN threads.\n\n```bash\nphp vendor/bin/server run 0.0.0.0:8000 --workers=8\n```\n\nYou can guess the number of physical threads your host has by using the value \n`-1`. By default, a single worker will be used.\n\nThis feature is not designed and intended for production environments. We \nencourage to use a reversed proxy or a small balancer if you need to balance \nbetween several processes. Furthermore, this feature uses `pcntl_fork`, so as\nthe documentation explains it is not available for Windows users.\n\n## Watcher\n\nYou can use the watcher by installing the `seregazhuk/php-watcher` dependency\nin your composer.\n\n```json\n\"require-dev\": {\n    \"seregazhuk/php-watcher\": \"*\"\n}\n```\n\nAfter installing the dependency, you will be able to start your server by \nchecking code changes.\n\n```bash\nphp vendor/bin/server watch 0.0.0.0:8000\n```\n\nThis feature is for development only.\n\n## Static server\n\nThis server can serve static files as well located in your project. By default,\nan adapter will provide a path where static files should be found (like DriftPHP\nstatics are located under `public/` folder), but you can overwrite this value,\nor even override it.\n\n```bash\nphp vendor/bin/server watch 0.0.0.0:8000 --static-folder=/my/own/folder/\nphp vendor/bin/server watch 0.0.0.0:8000 --no-static-folder\n```\n\nYou can create an alias as well if you need it. That can be useful if you want\nto mask the internal path with an external one, only exposing this second one.\nBoth values must be separated by the symbol `:`, being the first part the alias,\nand the second one the internal path.\n\n```bash\nphp vendor/bin/server watch 0.0.0.0:8000 --static-folder=/public/:/internal/public/path\n```\n\nIn this example, a file named `app.js` located under `/internal/public/path/` \nfolder will be accessible at `http://localhost:8000/public/app.js`. By default,\nthis feature is disabled.\n\n### Static server cache\n\nYou can define your static folder cache by adding a YAML file wherever you want and\nreferencing it when starting the server.\n\n```bash\nphp vendor/bin/server watch 0.0.0.0:8000 --static-cache=/my/path/static.cache.yml\n```\n\nIn this file you can define specific headers for all your static resources (in fact,\nnot only cache ones). You can define as well regular expressions for matching them.\n\n```yaml\n/public/js/.*:\n  Cache-Control: max-age=31536000\n/public/css/app.js:\n  Content-Type: application/javascript\n/public/images/logo.png:\n  Other-Header: Other-Value\n```\n\nBy default, this feature is disabled and no extra headers will be added into your static\nresources.\n\n### Important\n\nBy default, this package will not install the `react/filesystem` package. This\nmeans that, if you don't install it by hand in your project, all the disk \noperations will be blocking. These operations done synchronously will be much\nfaster and efficient, but by using large size files could slow down the entire \nprocess.\n\n## Symfony bridge\n\nIn order to help you from migrating an application from Symfony to DriftPHP, \nassuming that this means that your whole domain should turn on top of Promises, \nincluding your infrastructure layer, this server is distributed with a small\nSymfony adapter. Use it as a tool, and never use it at production (using a\nReactPHP based server in a blocking application is something not recommendable\nat all in terms of performance and service availability). That adapter will help\nyour migrating from one platform to the other, as will allow this server to work\nwith your Symfony kernel.\n\n```bash\nphp vendor/bin/server watch 0.0.0.0:8000 --adapter=symfony\n```\n\n## DriftPHP resources\n\nSome first steps for you!\n\n- [Go to DOCS](https://driftphp.io/#/?id=the-server)\n\nor\n\n- [Try a demo](https://github.com/driftphp/demo)\n- [Install the skeleton](https://github.com/driftphp/skeleton)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdriftphp%2Fserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdriftphp%2Fserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdriftphp%2Fserver/lists"}