Top Related Projects
A non-blocking concurrency framework for PHP applications. 🐘
Event-driven, non-blocking I/O with PHP.
🚀 Coroutine-based concurrency library for PHP
Event package for your app and domain
Icicle is a PHP library for writing asynchronous code using synchronous coding techniques
Quick Overview
Kraken-php/framework is an open-source, event-driven, non-blocking PHP framework designed for building scalable and high-performance applications. It focuses on asynchronous programming and provides tools for creating distributed systems, microservices, and parallel processing solutions.
Pros
- Asynchronous and non-blocking architecture for improved performance
- Built-in support for distributed systems and microservices
- Comprehensive set of components for various application needs
- Designed with scalability in mind
Cons
- Steep learning curve for developers unfamiliar with asynchronous programming
- Limited community support compared to more mainstream PHP frameworks
- Requires PHP 7.0 or higher, which may not be available on all hosting environments
- Documentation could be more extensive and up-to-date
Code Examples
- Creating a simple HTTP server:
use Kraken\Framework\Network\Http\HttpServer;
use Kraken\Framework\Network\Http\HttpRequest;
use Kraken\Framework\Network\Http\HttpResponse;
$server = new HttpServer('0.0.0.0:8080');
$server->on('request', function(HttpRequest $request, HttpResponse $response) {
$response->writeHead(200, ['Content-Type' => 'text/plain']);
$response->end('Hello World!');
});
$server->start();
- Implementing a basic pub/sub system:
use Kraken\Framework\Channel\ChannelInterface;
use Kraken\Framework\Channel\ChannelCompositeInterface;
$channel = $container->make(ChannelCompositeInterface::class);
$channel->on('message', function($data) {
echo "Received: $data\n";
});
$channel->publish('test', 'Hello, Kraken!');
- Creating a simple asynchronous task:
use Kraken\Framework\Core\CoreInterface;
$loop = $container->make(CoreInterface::class)->getLoop();
$loop->addTimer(1, function() {
echo "This message will be printed after 1 second.\n";
});
$loop->run();
Getting Started
To get started with Kraken-php/framework:
-
Install via Composer:
composer require kraken-php/framework
-
Create a new PHP file (e.g.,
app.php
) and add the following code:
<?php
require 'vendor/autoload.php';
use Kraken\Framework\Core\Core;
$core = new Core();
$core->run(function($core) {
echo "Kraken is running!\n";
$core->stop();
});
- Run the application:
php app.php
This will set up a basic Kraken application that prints a message and then exits.
Competitor Comparisons
A non-blocking concurrency framework for PHP applications. 🐘
Pros of amp
- Lightweight and focused on asynchronous programming
- Active development with frequent updates and releases
- Extensive documentation and examples
Cons of amp
- Less comprehensive than a full framework
- Steeper learning curve for developers new to asynchronous programming
- Limited built-in features compared to full-stack frameworks
Code Comparison
amp:
$promise = Amp\call(function () {
$response = yield Amp\Http\Client\HttpClientBuilder::buildDefault()
->request('https://example.com');
return yield $response->getBody()->buffer();
});
framework:
$client = $container->make('http.client');
$response = $client->get('https://example.com');
$body = $response->getBody();
Summary
amp is a lightweight library focused on asynchronous programming, offering active development and extensive documentation. However, it may have a steeper learning curve and fewer built-in features compared to framework. framework provides a more comprehensive solution but may be less flexible for specific asynchronous tasks. The code comparison shows amp's use of promises and yields for asynchronous operations, while framework uses a more traditional synchronous approach.
Event-driven, non-blocking I/O with PHP.
Pros of ReactPHP
- More mature and widely adopted project with a larger community
- Offers a broader ecosystem of components for various async operations
- Better documentation and more extensive examples available
Cons of ReactPHP
- Steeper learning curve for developers new to asynchronous programming
- Requires more manual configuration and setup compared to Kraken's out-of-the-box approach
- Less opinionated, which may lead to inconsistencies in application structure
Code Comparison
ReactPHP example:
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server('127.0.0.1:8080', $loop);
$socket->on('connection', function (React\Socket\ConnectionInterface $conn) {
$conn->write("Hello World!\n");
});
$loop->run();
Kraken example:
$loop = Loop::create();
$server = new Server(8080);
$server->onConnection(function(Connection $conn) {
$conn->send("Hello World!\n");
});
$loop->run();
Both frameworks provide similar functionality for creating a simple server, but Kraken's API is slightly more concise and intuitive. ReactPHP offers more flexibility in configuration, while Kraken provides a more streamlined approach with its pre-configured components.
🚀 Coroutine-based concurrency library for PHP
Pros of Swoole
- Higher performance due to its C-based implementation
- Built-in support for WebSockets, HTTP server, and asynchronous I/O
- Extensive documentation and active community support
Cons of Swoole
- Steeper learning curve for PHP developers unfamiliar with C extensions
- Requires compilation and installation of the extension
- May not be compatible with all PHP frameworks and libraries
Code Comparison
Swoole example:
$server = new Swoole\HTTP\Server('127.0.0.1', 9501);
$server->on('request', function ($request, $response) {
$response->end('<h1>Hello World</h1>');
});
$server->start();
Kraken example:
$loop = Loop::create();
$server = new Server($loop);
$server->on('request', function ($request, $response) {
$response->end('<h1>Hello World</h1>');
});
$server->listen(9501);
$loop->run();
Both examples create a simple HTTP server, but Swoole's implementation is more concise and doesn't require an external event loop. Kraken uses a more traditional PHP approach, which may be more familiar to some developers but potentially less performant.
Event package for your app and domain
Pros of Event
- Lightweight and focused solely on event handling
- Easier to integrate into existing projects due to its simplicity
- More frequent updates and active maintenance
Cons of Event
- Limited scope compared to Framework's comprehensive features
- Lacks built-in asynchronous processing capabilities
- May require additional libraries for complex event-driven architectures
Code Comparison
Event:
$emitter = new League\Event\Emitter;
$emitter->addListener('user.created', function (UserCreatedEvent $event) {
// Handle user creation
});
$emitter->emit(new UserCreatedEvent($user));
Framework:
$loop = new Kraken\Loop\Model\StreamSelectLoop;
$loop->addPeriodicTimer(0.1, function () {
// Perform periodic task
});
$loop->run();
Summary
Event is a focused library for event handling, making it easier to integrate into existing projects. However, it lacks the comprehensive features and asynchronous capabilities of Framework. Event is better suited for simpler event-driven applications, while Framework offers a more robust solution for complex, asynchronous systems. The choice between the two depends on the specific requirements of your project and the level of complexity you need to handle.
Icicle is a PHP library for writing asynchronous code using synchronous coding techniques
Pros of Icicle
- Lightweight and focused on asynchronous programming
- Simpler learning curve for developers familiar with PHP
- Active development and community support
Cons of Icicle
- Less comprehensive feature set compared to Kraken
- May require additional libraries for certain functionalities
- Smaller ecosystem and fewer extensions
Code Comparison
Icicle example:
$socket = yield Socket\connect('example.com', 80);
yield $socket->write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n");
$response = yield $socket->read();
Kraken example:
$loop = $container->make('Kraken\Loop\LoopInterface');
$loop->onTick(function() {
echo "Tick!\n";
});
$loop->start();
Summary
Icicle is a lightweight, asynchronous PHP framework focused on simplicity and ease of use. It's well-suited for developers looking to implement asynchronous programming in PHP without a steep learning curve. However, it may lack some of the more advanced features and comprehensive ecosystem found in Kraken.
Kraken, on the other hand, offers a more robust and feature-rich environment for building complex, distributed applications. It provides a wider range of tools and abstractions but may require more time to master fully.
The choice between the two depends on the specific project requirements, team expertise, and desired level of abstraction and features.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Kraken PHP Framework ~ Release the Kraken!
Note: This repository contains the core of the Kraken Framework. If you want to start developing new application with Kraken, check out Kraken Application Skeleton. If you want to learn more visit official website.
Description
Kraken is the first and only multi-processed, multi-threaded, fault-tolerant framework for PHP. It has been written to provide easy and reliable API for creating distributed applications using PHP. Kraken aims to solve typical problems of writing such applications and to provide developers with powerful yet elegant tools for dealing with them.
The main focus of Kraken Framework is put on:
- Concurrency : create systems that are asynchronous and concurrent by design,
- Distribution : divide your application into several containers and run them on multiple threads, processors or hosts,
- Fault tolerance : write systems that self-heal using remote and local supervision hierarchies,
- Elasticity : modify existing architecture in realtime without need to change in code,
- High performance : handle up to thousands of connections per second on each container,
- Extensibility : use available options to easily extend and adapt framework features for your needs.
Start writing applications that were previously marked as impossible or hard to implement in PHP right know. Servers, service-oriented architecture, agent-based models, games, complex daemons, socket programs, schedulers and much, much more - nothing is impossible with Kraken!
Feature Highlights
Kraken features:
- Support for asynchronous programming using fully-featured event Loop with multiple backgrounds.
- Support for event-driven architecture.
- Easy to understand and work with Promise-based API.
- Consistent multi-processing and multi-threading.
- Process and Thread abstraction as isolated message-driven containers.
- Built-in message routing system and IPC abstraction.
- Configurable local and remote supervision hierarchies.
- Centralized deployment and management.
- Extensible Console and Server interface.
- Asynchronous TCP and UDP sockets.
- Asynchronous Stream wrappers.
- Standalone HTTP and WebSocket server.
- Variety of IPC models.
- ReactPHP-compatibility adapters.
- ...and more.
Full list of features can be found on official website.
Performance
Kraken is able to emit millions of events and thousands of messages and connections per second using single container. It is scalable for multiple processes and threads, faster than traditional PHP approach and able to handle similar amount of connections as Node.js.
Note: Keep in mind that Kraken project does not solely focus around HTTP performance. It provides a set of distinct asynchronous libraries to use in PHP. The attached graph's main intention is to show that PHP is fast enough to compete with the leading technologies available on the market. The HTTP component has been chosen as it is the only one that can be easily compared between asynchronous and synchronous MVC frameworks. Do not treat it as an actual benchmark.
Powered By
Kraken Framework is built on top of asynchronous library named Dazzle Project. If you are looking for a solution simpler than framework, you might consider using it instead.
Requirements
- PHP-5.6 or PHP-7.0+,
- UNIX or Windows OS,
- Additional constraints based on which components you do plan to use.
Installation and Official Documentation
Documentation for the framework can be found in the official documentation page. To see installation instructions, please check out application skeleton or go to installation guide.
Examples
There are few examples you can try, before deciding to use Kraken:
- Chat Application.
- Symfony Integration & Websockets Bundle.
- Symfony Integration & Websockets Bundle Example.
If you have written your own demo application for Kraken, and want to list it here, contact us!
Frequently Asked Questions
How does the Kraken differ from other PHP async libraries?
In comparison to already existing PHP async libraries, Kraken gives the developer not only the async tools, but also do the most of the dirty work of creating distributed applications. It gives you consistent interface for working with processes and threads, implements fault-tolerance mechanisms, allows usage of remote and local supervision hierarchies, gives IPC abstractions and implements most important messaging patterns such as routing, heartbeating and much, much more. Instead of thinking how to use async libraries in the new project and how to design the project with them, developer can simply start Kraken instance and focus on its business logic!
Is PHP GC able to handle daemonized, long-running application?
In most cases yes, but developers still have to keep an eye on application memory usage. PHP 5.5+, which is required for using framework, is able to successfully deal with proper memory handling and garbage collecting. It proves to be true regarding Kraken modules as in development cycle special attention was paid to ensure they do not leak memory. This, however, cannot be guaranteed with usage of third party vendors as some (ex. ORMs) are known to be prone to this problem. The easiest way to avoid it is to simply use destructors, unset functions and create memory allocation and deallocation tests. If despite that you still stumble across this problem, the best way to deal with it is to isolate leaking piece of code in separated container and restart it cyclically when it reaches its memory limits.
If there are any additional questions that you have about framework, please check whether the answers for them have been already posted in issues or ask on our gitter room.
Contributing
Thank you for considering contributing to Kraken Framework! The contribution guide can be found in the contribution tips.
License
Kraken Framework is open-sourced software licensed under the MIT license. The documentation is provided under FDL-1.3 license.
Top Related Projects
A non-blocking concurrency framework for PHP applications. 🐘
Event-driven, non-blocking I/O with PHP.
🚀 Coroutine-based concurrency library for PHP
Event package for your app and domain
Icicle is a PHP library for writing asynchronous code using synchronous coding techniques
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot