Getting started with Swoole_php instances in PHP

Source: Internet
Author: User
Tags website performance
Swoole is a PHP advanced Web development framework. it is not designed to improve website performance, but to improve website development efficiency. In exchange for the highest development efficiency, Swoole is a PHP advanced Web development framework. The framework is not designed to improve the website performance, but to improve the website development efficiency. Minimum performance loss in exchange for maximum development efficiency. Using the Swoole framework, you can develop a complex Web function in a short time.

Official definition:

Swoole: redefine PHP

PHP's asynchronous, parallel, and high-performance network communication engine, written in pure C language, provides PHP-language asynchronous multi-thread servers, asynchronous TCP/UDP network clients, asynchronous MySQL, asynchronous Redis, database connection pool, AsyncTask, message queue, millisecond timer, asynchronous file read/write, asynchronous DNS query. Swoole has built-in Http/WebSocket server/client and Http2.0 server.

Swoole can be widely used in the Internet, mobile communication, enterprise software, cloud computing, online games, IoT, IoV, smart home, and other fields. Using PHP + Swoole as the network communication framework can greatly improve the efficiency of enterprise it r & D teams and focus more on developing innovative products.

Swoole extension installation and case study Source:

Simple case:

<? Phpclass Server {private $ serv; public function _ construct () {$ this-> serv = new swoole_server ("", 9501 ); $ this-> serv-> set (array ('worker _ num' => 8, 'daemonize '=> false, 'max _ request' => 10000, 'Dispatch _ mode' => 2, 'debug _ mode' => 1); $ this-> serv-> on ('start', array ($ this, 'onstart'); $ this-> serv-> on ('connect ', array ($ this, 'onconnect ')); $ this-> serv-> on ('receive ', array ($ this, 'onreceive'); $ this-> s Erv-> on ('close', array ($ this, 'onclose'); $ this-> serv-> start ();} public function onStart ($ serv) {echo "Start \ n";} public function onConnect ($ serv, $ fd, $ from_id) {$ serv-> send ($ fd, "Hello {$ fd }! ");} Public function onReceive (swoole_server $ serv, $ fd, $ from_id, $ data) {echo" Get Message From Client {$ fd }: {$ data} \ n ";} public function onClose ($ serv, $ fd, $ from_id) {echo" Client {$ fd} close connection \ n ";}} // start the server $ Server = new server (); <? Phpclass Client {private $ client; public function _ construct () {$ this-> client = new swoole_client (SWOOLE_SOCK_TCP);} public function connect () {if (! $ This-> client-> connect ("", 9501, 1) {echo "Error: {$ fp-> errMsg} [{$ fp-> errCode}] \ n ";}$ message = $ this-> client-> recv (); echo "Get Message From Server: {$ message} \ n"; fwrite (STDOUT, "Enter the Message:"); $ msg = trim (fgets (STDIN )); $ this-> client-> send ($ msg) ;}$ client = new Client (); $ client-> connect ();

Open two terminal inputs: php server. php client. php to see the effect!

Swoole features

It includes the following features:

1. Data Query similar to ORM. It provides an SQL package to seamlessly integrate MySQL SQL with PHP Array, session, and Cache.

2. application MVC layered structure, effective program structural layering, improving program maintainability and scalability, achieving low coupling, and interface-based development.

3. integrate a large number of practical functions, such as convenient database operations, template operations, cache operations, system configuration, form processing, paging, data calling, dictionary operations, and upload processing, content editing and debugging.

4. template-the data reflection system can call data directly in the template and provide many tags. However, you can update and maintain the website only by modifying the template without modifying the program.

Other functions

1. Swoole contains a large number of classes and provides many function Extensions. basically, most of the function classes that can be used in Web development can be found in the Swoole framework.

2. Swoole has a plug-in system, Fckeditor, Adodb, pscws Chinese word segmentation, Chinese full text index system, the latest Key-Value database ideas, TokyoTyrant, which can infinitely expand the functions of the framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.