Running a Full Laravel Application on Mobile via php-wasm: Challenges and Solutions
Jefferson Silva, creator of NativeBlade, has successfully embedded a complete Laravel application within php-wasm on a mobile device, eliminating the need for a server. This approach involves running the entire Laravel + Livewire app, including vendor files, within a WebView on the device. The application is packaged as a single JSON file containing approximately 6,200 files (40 MB as JSON, 7 MB gzipped), with Livewire alone accounting for 8 MB of that size. The core challenge lies in PHP’s synchronous nature conflicting with the asynchronous environment of web assembly (wasm). To address this, Silva implemented a unique solution where PHP requests are handled through a series of re-executions. When a request requires external resources (like HTTP calls or database queries), the PHP process writes a pending request to a temporary file, exits, and the shell (the host environment) processes the request asynchronously. The response is then cached, and the PHP process is re-executed to continue from where it left off. This method ensures that Laravel functions as expected, but it introduces complexities such as side effects occurring on every re-execution and the need for careful handling of asynchronous operations. The article details how NativeBlade manages these challenges, including handling cancellations, inlining assets due to the lack of a file server, and integrating native device features through JSON-based communication. The author emphasizes that this approach keeps PHP and Laravel intact while containing the complexity within a small set of bridge files. The project is open source, with code and documentation available on GitHub and the NativeBlade documentation site. This work demonstrates the potential of php-wasm to run full-stack applications on mobile devices without server dependencies, though it requires significant adaptation of standard PHP practices.
