Awesome
Swoole by Examples
The repository is to help developers to get familiar with Swoole through a variety of examples. All the examples are fully functioning; they can be executed and verified using the Docker images provided.
NOTE: I'm adding examples for latest versions of Swoole, so please be patient.
Setup the Development Environment
We use Docker to setup our development environment. Other than Docker, you don't need to install any other software to run and test the examples: you don't need to have PHP, Swoole, Composer, or some other software installed locally.
We use the official Docker image of Swoole to run the examples. There are tens of examples under repository swoole/docker-swoole shown how to use the image. Please spend some time checking it first.
Before running the examples, please run command docker-compose up -d
under the root repository directory to start the
Docker containers. There are two containers used to run the examples:
- a server container where application servers are running.
- a client container where client-side scripts should be executed.
Both containers have the same PHP scripts in place, so most standalone scripts (e.g., most CSP programming examples) can be executed from either container. Once the containers are running, you can use one of following commands to get a Bash shell in the containers:
docker compose exec -ti server bash # Get a Bash shell in the server container.
docker compose exec -ti client bash # Get a Bash shell in the client container.
List of Examples
- CSP programming
- from blocking I/O to non-blocking I/O
- The blocking version can be found here.
- The non-blocking version of the same script can be found here. You can also check this script to see how the non-blocking version is executed in order.
- This example shows how the return statement is treated differently in Swoole. As you can see in the example, a function call could return a value back first before finishing its execution.
- coroutines
- create coroutines
- enable coroutines
- coroutines in a for loop
- nested coroutines
- exit from coroutines
- yield and resume coroutines
- context: How to use the Context objects in coroutines.
- benchmark: In this example we create 1,000,000 coroutines in a single process; each coroutine sleeps for 5 seconds.
- channels
- defer
- runtime hooks
- curl. There are two different ways to hook curl functions:
- Option SWOOLE_HOOK_NATIVE_CURL (recommended)
- Option SWOOLE_HOOK_CURL: This approach is implemented through Swoole Library; however, it doesn't work for curl_multi*_ functions.
- MySQL
- curl. There are two different ways to hook curl functions:
- deadlocks
- examples on deadlocks
- pop data from an empty channel
- push data to a full channel
- try to lock a locked file while the existing lock never gets released
- acquire a locked lock from another coroutine
- improperly shutdown or reload a server
- When the only coroutine yields its execution. The examples are shown in the next section when we talk about
How to detect/handle deadlocks
.
- How to detect/handle deadlocks. In the following examples, we trigger deadlocks by yielding the execution of the only coroutine in the program.
- examples on deadlocks
- advanced topics
- CPU-intensive job scheduling
- class \Swoole\Coroutine\Barrier
- block coroutines/processes
- unit tests
- from blocking I/O to non-blocking I/O
- server-side programming
- application servers
- HTTP/1 server: support gzip compression, serving static content, customizing status code, etc.
- HTTP/2 server
- HTTP/2 server push
- WebSocket server
- Redis server
- proxy server
- TCP server
- UDP server
- resource pooling
- process pool
- connection pool
- Redis connection pool
- How to implement a customized connection pool? Check package crowdstar/vertica-swoole-adapter for details. This package implements connection pool for HP Vertica databases through ODBC, and it's maintained by me.
- network connection detection (dead network detection)
- task scheduling and handling
- timer
- There is a 2nd example included to show how to implement timer using coroutines only.
- To see how to setup cronjobs using the \Swoole\Timer class in an application server, please check integrated HTTP/1 server.
- timer
- cronjobs
- benchmark
- base mode vs multi-process mode
- advanced topics
- Rock Paper Scissors: implement the hand game Rock Paper Scissors using Swoole.
- How are different server events triggered?
- Server Combo (two different implementations)
- integrated HTTP/1 server: an HTTP/1 server that supports cron jobs and synchronous/asynchronous tasks.
- integrated WebSocket server: a WebSocket server that supports cron jobs and asynchronous tasks. This implementation use separate processes to handle cron jobs and task queues.
- mixed protocols
- support HTTP/1, HTTP/2, and WebSocket on same port
- support multiple protocols on same server
- DDoS protection: How to protect your Swoole-based application server from DDoS attacks.
- interruptible sleep: This example shows how to set up a cronjob in a web server, and allow the cronjob to execute at a last time when the server is shutting down.
- multiple ports listening
- application servers
- client-side programming
- HTTP/1 client
- HTTP/2 client
- WebSocket client
- TCP client
- UDP client
- PostgreSQL client
- miscellaneous topics
- data management in Swoole: globals, persistence, and caching
- APCu caching: APCu caching in Swoole works the same way as in other PHP CLI applications. This example explains it in details.
- atomic counters
- multiprocessing
- wait and wakeup processes
- process pool
- pool creation and inter-process communication: Please check previous section
resource pooling
for details. - detach processes from a process pool
- pool creation and inter-process communication: Please check previous section
- data management in Swoole: globals, persistence, and caching