Download the PHP package zd04/laravel-s without Composer
On this page you can find all versions of the php package zd04/laravel-s. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-s
π LaravelS is
an out-of-the-box adapter
between Swoole and Laravel/Lumen.
Please Watch
this repository to get the latest updates.
Table of Contents
- Features
- Requirements
- Install
- Run
- Deploy
- Cooperate with Nginx (Recommended)
- Cooperate with Apache
- Enable WebSocket server
- Listen events
- System events
- Customized asynchronous events
- Asynchronous task queue
- Millisecond cron job
- Automatically reload after modifying code
- Get the instance of SwooleServer in your project
- Use SwooleTable
- Multi-port mixed protocol
- Coroutine
- Custom process
- Other features
- Important notices
- Users and cases
- Alternatives
- Sponsor
- License
Features
-
Built-in Http/WebSocket server
-
Memory resident
-
Gracefully reload
-
Support Laravel/Lumen both, good compatibility
- Simple & Out of the box
Requirements
Dependency | Requirement |
---|---|
PHP | >= 5.5.9 Recommend PHP7+ |
Swoole | >= 1.7.19 No longer support PHP5 since 2.0.12 Recommend 4.2.3+ |
Laravel/Lumen | >= 5.1 Recommend 5.6+ |
Install
1.Require package via Composer(packagist).
2.Register service provider(pick one of two).
-
Laravel
: inconfig/app.php
file,Laravel 5.5+ supports package discovery automatically, you should skip this step
Lumen
: inbootstrap/app.php
file
3.Publish configuration and binaries.
After upgrading LaravelS, you need to republish; click here to see the change notes of each version.
4.Change config/laravels.php
: listen_ip, listen_port, refer Settings.
Run
Please read the notices carefully before running
, Important notices(IMPORTANT).
- Commands:
php bin/laravels {start|stop|restart|reload|info|help}
.
Command | Description |
---|---|
start | Start LaravelS, list the processes by "ps -ef|grep laravels". Support the option "-d|--daemonize" to run as a daemon; Support the option "-e|--env" to specify the environment to run, such as --env=testing will use the configuration file .env.testing firstly, this feature requires Laravel 5.2+ |
stop | Stop LaravelS |
restart | Restart LaravelS, support the options "-d|--daemonize" and "-e|--env" |
reload | Reload all Task/Worker/Timer processes which contain your business codes, and trigger the method onReload of Custom process, CANNOT reload Master/Manger processes. After modifying config/laravels.php , you can only call restart to restart |
info | Display component version information |
help | Display help information |
Runtime
files:start
will automatically executeartisan laravels config
and generate these files, developers generally don't need to pay attention to them, it's recommended to add them to.gitignore
.
File | Description |
---|---|
storage/laravels.json | LaravelS's runtime configuration file |
storage/laravels.pid | PID file of Master process |
storage/laravels-timer-process.pid | PID file of the Timer process |
storage/laravels-custom-processes.pid | PID file of all custom processes |
Deploy
It is recommended to supervise the main process through Supervisord, the premise is without option
-d
and to setswoole.daemonize
tofalse
.
Cooperate with Nginx (Recommended)
Demo.
Cooperate with Apache
Enable WebSocket server
The Listening address of WebSocket Sever is the same as Http Server.
1.Create WebSocket Handler class, and implement interface WebSocketHandlerInterface
.The instant is automatically instantiated when start, you do not need to manually create it.
2.Modify config/laravels.php
.
3.Use SwooleTable
to bind FD & UserId, optional, Swoole Table Demo. Also you can use the other global storage services, like Redis/Memcached/MySQL, but be careful that FD will be possible conflicting between multiple Swoole Servers
.
4.Cooperate with Nginx (Recommended)
Refer WebSocket Proxy
5.Heartbeat setting
-
Heartbeat setting of Swoole
- Proxy read timeout of Nginx
6.Push data in controller
Listen events
System events
Usually, you can reset/destroy some
global/static
variables, or change the currentRequest/Response
object.
-
laravels.received_request
After LaravelS parsedSwoole\Http\Request
toIlluminate\Http\Request
, before Laravel's Kernel handles this request. laravels.generated_response
After Laravel's Kernel handled the request, before LaravelS parsesIlluminate\Http\Response
toSwoole\Http\Response
.
Customized asynchronous events
This feature depends on
AsyncTask
ofSwoole
, your need to setswoole.task_worker_num
inconfig/laravels.php
firstly. The performance of asynchronous event processing is influenced by number of Swoole task process, you need to set task_worker_num appropriately.
1.Create event class.
2.Create listener class.
3.Fire event.
Asynchronous task queue
This feature depends on
AsyncTask
ofSwoole
, your need to setswoole.task_worker_num
inconfig/laravels.php
firstly. The performance of task processing is influenced by number of Swoole task process, you need to set task_worker_num appropriately.
1.Create task class.
2.Deliver task.
Millisecond cron job
Wrapper cron job base on Swoole's Millisecond Timer, replace
Linux
Crontab
.
1.Create cron job class.
2.Register cron job.
3.Note: it will launch multiple timers when build the server cluster, so you need to make sure that launch one timer only to avoid running repetitive task.
4.LaravelS v3.4.0
starts to support the hot restart [Reload] Timer
process. After LaravelS receives the SIGUSR1
signal, it waits for max_wait_time
(default 5) seconds to end the process, then the Manager
process will pull up the Timer
process again.
5.If you only need to use minute-level
scheduled tasks, it is recommended to enable Hhxsv5\LaravelS\Illuminate\LaravelScheduleJob
instead of Linux Crontab, so that you can follow the coding habits of Laravel task scheduling and configure Kernel
.
Automatically reload after modifying code
-
Via
inotify
, support Linux only.1.Install inotify extension.
2.Turn on the switch in Settings.
3.Notice: Modify the file only in
Linux
to receive the file change events. It's recommended to use the latest Docker. Vagrant Solution. -
Via
fswatch
, support OS X/Linux/Windows.1.Install fswatch.
2.Run command in your project root directory.
-
Via
inotifywait
, support Linux.1.Install inotify-tools.
2.Run command in your project root directory.
- When the above methods does not work, the ultimate solution: set
max_request=1,worker_num=1
, so thatWorker
process will restart after processing a request. The performance of this method is very poor,so only development environment use
.
Get the instance of SwooleServer
in your project
Use SwooleTable
1.Define Table, support multiple.
All defined tables will be created before Swoole starting.
2.Access Table
: all table instances will be bound on SwooleServer
, access by app('swoole')->xxxTable
.
Multi-port mixed protocol
For more information, please refer to Swoole Server AddListener
To make our main server support more protocols not just Http and WebSocket, we bring the feature multi-port mixed protocol
of Swoole in LaravelS and name it Socket
. Now, you can build TCP/UDP
applications easily on top of Laravel.
-
Create socket handler class, and extend
Hhxsv5\LaravelS\Swoole\Socket\{TcpSocket|UdpSocket|Http|WebSocket}
.These
Socket
connections share the same worker processes with yourHTTP
/WebSocket
connections. So it won't be a problem at all if you want to deliver tasks, useSwooleTable
, even Laravel components such as DB, Eloquent and so on. At the same time, you can accessSwoole\Server\Port
object directly by member propertyswoolePort
. -
Register Sockets.
About the heartbeat configuration, it can only be set on the
main server
and cannot be configured onSocket
, but theSocket
inherits the heartbeat configuration of themain server
.For TCP socket,
onConnect
andonClose
events will be blocked whendispatch_mode
of Swoole is1/3
, so if you want to unblock these two events please setdispatch_mode
to2/4/5
. - Test.
-
TCP:
telnet 127.0.0.1 5291
- UDP: [Linux]
echo "Hello LaravelS" > /dev/udp/127.0.0.1/5292
-
Register example of other protocols.
-
UDP
-
Http
- WebSocket: The main server must
turn on WebSocket
, that is, setwebsocket.enable
totrue
.
-
Coroutine
-
Warning: The order of code execution in the coroutine is out of order. The data of the request level should be isolated by the coroutine ID. However, there are many singleton and static attributes in Laravel/Lumen, the data between different requests will affect each other, it's
Unsafe
. For example, the database connection is a singleton, the same database connection shares the same PDO resource. This is fine in the synchronous blocking mode, but it does not work in the asynchronous coroutine mode. Each query needs to create different connections and maintain IO state of different connections, which requires a connection pool. SoDO NOT
enable the coroutine, only the custom process can use the coroutine. -
Enable Coroutine, default disable.
-
Coroutine Client: require
Swoole>=2.0
. - Runtime Coroutine: require
Swoole>=4.1.0
, and enable it.
Custom process
Support developers to create special work processes for monitoring, reporting, or other special tasks. Refer addProcess.
-
Create Proccess class, implements CustomProcessInterface.
-
Register TestProcess.
-
Note: The callback() cannot quit. If quit, the Manager process will re-create the process.
- Example: Write data to a custom process.
Other features
Configuring the event callback function of Swoole
Supported events:
Event | Interface | When happened |
---|---|---|
ServerStart | Hhxsv5\LaravelS\Swoole\Events\ServerStartInterface | Occurs when the Master process is starting, this event should not handle complex business logic, and can only do some simple work of initialization . |
ServerStop | Hhxsv5\LaravelS\Swoole\Events\ServerStopInterface | Occurs when the server exits normally, CANNOT use async or coroutine related APIs in this event . |
WorkerStart | Hhxsv5\LaravelS\Swoole\Events\WorkerStartInterface | Occurs after the Worker/Task process is started, and the Laravel initialization has been completed. |
WorkerStop | Hhxsv5\LaravelS\Swoole\Events\WorkerStopInterface | Occurs after the Worker/Task process exits normally |
WorkerError | Hhxsv5\LaravelS\Swoole\Events\WorkerErrorInterface | Occurs when an exception or fatal error occurs in the Worker/Task process |
1.Create an event class to implement the corresponding interface.
2.Configuration.
Important notices
-
Singleton Issue
-
Under FPM mode, singleton instances will be instantiated and recycled in every request, request start=>instantiate instance=>request end=>recycled instance.
-
Under Swoole Server, All singleton instances will be held in memory, different lifetime from FPM, request start=>instantiate instance=>request end=>do not recycle singleton instance. So need developer to maintain status of singleton instances in every request.
-
Common solutions:
-
Write a
XxxCleaner
class to clean up the singleton object state. This class implements the interfaceHhxsv5\LaravelS\Illuminate\Cleaners\CleanerInterface
and then registers it incleaners
oflaravels.php
. -
Reset
status of singleton instances byMiddleware
. - Re-register
ServiceProvider
, addXxxServiceProvider
intoregister_providers
of filelaravels.php
. So that reinitialize singleton instances in every request Refer.
-
- LaravelS has built in some cleaners.
-
-
Known issues: a package of known issues and solutions.
-
Debugging method: Logging, Laravel Dump Server(Laravel 5.7 has been integrated by default).
-
Should get all request information from
Illuminate\Http\Request
Object, $_ENV is readable, $_SERVER is partially readable,CANNOT USE
$_GET/$_POST/$_FILES/$_COOKIE/$_REQUEST/$_SESSION/$GLOBALS. -
Respond by
Illuminate\Http\Response
Object, compatible with echo/vardump()/print_r()οΌCANNOT USE
functions dd()/exit()/die()/header()/setcookie()/http_response_code(). - The various
singleton connections
will bememory resident
, recommend to enablepersistent connection
.- Database connection, it
will
reconnect automaticallyimmediately
after disconnect.
- Database connection, it
- Redis connection, it
won't
reconnect automaticallyimmediately
after disconnect, and will throw an exception about lost connection, reconnect next time. You need to make sure thatSELECT DB
correctly before operating Redis every time.
-
global
,static
variables which you declared are need to destroy(reset) manually. -
Infinitely appending element into
static
/global
variable will lead to OOM(Out of Memory). - Pressure test
Alternatives
Sponsor
License
All versions of laravel-s with dependencies
symfony/console Version >=2.7.0
ext-json Version *
ext-pcntl Version *
swoole/ide-helper Version @dev