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.
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 exec -ti $(docker ps -qf "name=server") bash # Get a Bash shell in the server container.
docker exec -ti $(docker ps -qf "name=client") bash # Get a Bash shell in the client container.
- 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.
- coroutines
- enable coroutines
- create coroutines
- yield and resume coroutines
- coroutines in a for loop
- nested coroutines
- exit from coroutines
- context
- benchmark: In this example we create 1,000,000 coroutines in a single process; each coroutine sleeps for 5 seconds.
- channels
- defer
- advanced topics
- CPU-intensive job scheduling
- runtime hooks
- hook curl functions
- hook mysql/pdo functions
- 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.
- integrated HTTP/1 server: an HTTP/1 server that supports cronjobs and synchronous/asynchronous tasks.
- HTTP/2 server
- HTTP/2 server push
- WebSocket server
- Redis server
- proxy server
- TCP server
- UDP server
- HTTP/1 server: support gzip compression, serving static content, customizing status code, etc.
- 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
- benchmark
- single-process mode vs multi-process mode
- advanced topics
- mixed protocols
- support HTTP/1, HTTP/2, and WebSocket on same port
- support multiple protocols on same server
- multiple ports listening
- event listeners
- mixed protocols
- application servers
- client-side programming
- HTTP/1 client
- HTTP/2 client
- WebSocket client
- TCP client
- Swoole extensions
- async
- orm
- postgresql
- serialize
- zookeeper