Asynchronous WEB in 2018. We write a chat on Websocket using Swoole

 3r3755. 3r3-31. Asynchronous WEB in 2018. We write a chat on Websocket using Swoole Websocket `s
has already been touched on more than once in Habré, in particular, options for implementing PHP were considered. However, since the release of r3r39. the last article
With the review of different technologies more than a year has passed, and the world of PHP There is something like brag about the past tense. 3r3741.  3r3755. 3r3741.  3r3755. In this article I want to introduce the Russian-speaking community to Swoole - Asynchronous Open Source framework for PHP, written in C, and supplied as a pecl extension. 3r3741.  3r3755. 3r3741.  3r3755. You can see the resulting application (chat): here is . 3r3741.  3r3755. 3r33737. Sources on github
. 3r3741.  3r3755. 3r3331. 3r33744. 3r3741.  3r3755. 3r38282. Why Swoole?
3r3741.  3r3755. Surely there are people who, in principle, will be against using PHP for such purposes, but in favor of PHP they can often play: 3r3741.  3r3755. 3r3741.  3r3755.
 3r3755. 3r33737. Reluctance to breed a zoo of various languages ​​on the project
 3r3755. 3r33737. The ability to use the already established code base (if the project is in PHP). 3r33737.  3r3755. 3r33737. 3r3741.  3r3755. Nevertheless, even comparing with node.js /go /erlang and other languages ​​natively offering the asynchronous model, Swoole - a framework written in C and combining the low threshold of entry and powerful functionality can be quite a good candidate. 3r3741.  3r3755. 3r3741.  3r3755. 3r33394. Framework features: [/b] 3r3741.  3r3755. 3r3741.  3r3755.
 3r3755. 3r33737. Event, asynchronous programming model
 3r3755. 3r33737. Asynchronous TCP /UDP /HTTP /Websocket /HTTP2 client /server APIs
 3r3755. 3r33737. Support for IPv4 /IPv6 /Unixsocket /TCP /UDP and SSL /TLS
 3r3755. 3r33737. Fast serialization /deserialization of data
 3r3755. 3r33737. High performance, extensibility, support for up to 1 million simultaneous connections
 3r3755. 3r33737. Task Scheduler accurate to milliseconds
 3r3755. 3r33737. Open source 3r33737.  3r3755. 3r33737. Coroutines support 3r33737.  3r3755. 3r33737. 3r3741.  3r3755. 3r33394. Possible uses: [/b] 3r3741.  3r3755. 3r3741.  3r3755.
 3r3755. 3r33737. Microservices
 3r3755. 3r33737. Game servers
 3r3755. 3r33737. The Internet of Things
 3r3755. 3r33737. Live systems of communication
 3r3755. 3r33737. WEB API
 3r3755. 3r33737. Any other services from which instant response /high speed /asynchronous execution is required
 3r3755. 3r33737. 3r3741.  3r3755. Code samples can be seen at home page. . In the documentation section for more information about the entire functionality of the framework. 3r3741.  3r3755. 3r3741.  3r3755. 3r38282. Let's start using
3r3741.  3r3755. Below I will describe the process of writing a simple Websocket server for online chat and the possible difficulties. 3r3741.  3r3755. 3r3741.  3r3755. Before you begin: More information about grades 3-333140. swoole_websocket_server
and 3r3142. swoole_server
(The second class is inherited from the first). 3r3741.  3r3755. 3r33737. Sources of the chat. 3r33744. 3r3741.  3r3755. 3r3741.  3r3755. 3r3630. 3r3633. Installing the framework
3r33333. Linux users 3r3741.  3r3755. 3r3741.  3r3755. #! /bin /bash
 3r3755. pecl install swoole
 3r3755. 3r3741.  3r3755. Mac users
 3r3755. 3r3741.  3r3755. # get a list of avaiable packages
 3r3755. brew install swoole
 3r3755. #! /bin /bash
 3r3755. brew install homebrew /php /php71-swoole
 3r3755. 3r3670. 3r3741.  3r3755. 3r3751. 3r3751. 3r3741.  3r3755. To use autocomplex in IDE it is suggested to use 3r3186. ide-helper 3r3741.  3r3755. 3r3741.  3r3755. 3r33394. Minimum Websocket Server Template: [/b] 3r3741.  3r3755. 3r3741.  3r3755. 3r3638. 3r3640. $ server = new swoole_websocket_server ("???.1", 9502); 3r3755. 3r3755. $ server-> on ('open', function ($ server, $ req) {
echo "connection open: {$ req-> fd} n";
}); 3r3755. 3r3755. $ server-> on ('message', function ($ server, $ frame) {3r3755. echo "received message: {$ frame-> data} n"; 3r3755. $ server-> push ($ frame-> fd, json_encode (["hello", "world"]));
}); 3r3755. 3r3755. $ server-> on ('close', function ($ server, $ fd) {
echo "connection close: {$ fd} n";
}); 3r3755. 3r3755. $ server-> start (); 3r3755. 3r3670. 3r3741.  3r3755. $ fd is the connection identifier. 3r3741.  3r3755. Get current connections:
 3r3755. 3r3741.  3r3755. 3r3638. $ server-> connections; 3r3670. 3r3741.  3r3755. Inside the $ frame contains all the data sent. Here is an example of the incoming object in the onmessage function:
 3r3755. 3r3741.  3r3755. 3r3638. SwooleWebSocketFrame Object
W2w2w21. => 20
. 3r3670. 3r3741.  3r3755. The data is sent to the client using the function 3r3741.  3r3755. 3r3741.  3r3755. 3r3638. Server :: push ($ fd, $ data, $ opcode = null, $ finish = null) 3r3741.  3r3755. Learn more about frames and opcodes in Russian at learn.jаvascript . The section "data format" 3r3741.  3r3755. 3r3741.  3r3755. The maximum detail about the protocol Websocket - RFC 3r3741.  3r3755. 3r3741.  3r3755. 3r33394. How to save data that came to the server? 3r33232. 3r3741.  3r3755. Swoole provides functionality for asynchronous operation from 3r33276. MySQL , Redis , file I /O 3r3741.  3r3755. 3r3741.  3r3755. And also 3r33286. swoole_buffer , swoole_channel and swoole_table 3r3741.  3r3755. I think the differences are not difficult to understand the documentation. For storing usernames, I chose swoole_table. The messages themselves are stored in MySQL. 3r3741.  3r3755. 3r3741.  3r3755. So, initialization of the table of user names:
 3r3755. 3r3741.  3r3755. 3r3638. 3r3755. $ users_table = new swoole_table (131072); 3r3755. $ users_table-> column ('id', swoole_table :: TYPE_INT, 5); 3r3755. $ users_table-> column ('username', swoole_table :: TYPE_STRING, 64); 3r3755. $ users_table-> create (); 3r3755. 3r3670. 3r3741.  3r3755. Filling data is as follows:
 3r3755. 3r3741.  3r3755. 3r3638. $ count = count ($ messages_table); 3r3755. 3r3755. $ dateTime = time (); 3r3755. $ row =['username' => $username, 'message' => $data->message, 'date_time' => $dateTime]; 3r3755. $ messages_table-> set ($ count, $ row); 3r3755. 3r3670. 3r3741.  3r3755. To work with MySQL, I decided not to use the asynchronous model yet, but to use the standard method, from the web socket server, via PDO
 3r3755. 3r3741.  3r3755. 3r3630. 3r3633. Appeal to base [/b] 3r33333. 3r3638. /**
* @return Message[]3r3755. * /
public function getAll ()
$ stmt = $ this-> pdo-> query ('SELECT * from messages'); 3r3755. $ messages =[]; 3r3755. foreach ($ stmt-> fetchAll () as $ row) {
$ messages[]= new Message ($ row['username'], $ row['message'], new DateTime ($ row['date_time'])); 3r3755.}
return $ messages; 3r3755.}
3r3741.  3r3755. 3r3751. 3r3751. 3r3741.  3r3755. Websocket server, it was decided to issue in the form of a class, and start it in the constructor:
 3r3755. 3r3741.  3r3755. 3r3630. 3r3633. Constructor 3r33232. 3r33333. 3r3638. public function __construct ()
$ this-> ws = new swoole_websocket_server ('???.0', 9502); 3r3755. 3r3755. $ this-> ws-> on ('open', function ($ ws, $ request) {3r3755. $ this-> onConnection ($ request);
}); 3r3755. $ this-> ws-> on ('message', function ($ ws, $ frame) {3r3755. $ this-> onmessage ($ frame);
}); 3r3755. $ this-> ws-> on ('close', function ($ ws, $ id) {
$ this-> onClose ($ id);
}); 3r3755. 3r3755. $ this-> ws-> on ('workerStart', function (swoole_websocket_server $ ws) {
$ this-> onWorkerStart ($ ws);
}); 3r3755. 3r3755. $ this-> ws-> start (); 3r3755.}
3r3741.  3r3755. 3r3751. 3r3751. 3r3741.  3r3755. 3r33394. Problems encountered: [/b] 3r3741.  3r3755. 3r3741.  3r3755. 3r33400.  3r3755. 3r33737. The user connected to the chat breaks the connection after 60 seconds if there is no packet exchange (that is, the user did not send or receive anything) 3r33716.  3r3755. 3r33737. The web server loses connection with MySQL if no interaction takes place for a long time  3r3755. 3r3408. 3r3741.  3r3755. Solution: 3r3741.  3r3755. 3r3741.  3r3755. In both cases, we need the implementation of the “ping” function, which will constantly ping the client every n seconds in the first case, and the MySQL database in the second. 3r3741.  3r3755. 3r3741.  3r3755. Since both functions must work asynchronously, they must be called in the child processes of the server. 3r3741.  3r3755. 3r3741.  3r3755. To do this, they can be initialized at the «workerstart» event. We have already defined it in the constructor, and this event already calls the $ this-> onWorkerStart method:
 3r3755. The Websocket protocol supports ping-pong from the box. Below you can see the implementation on Swoole. 3r3741.  3r3755. 3r3741.  3r3755. 3r3630. 3r3633. onWorkerStart [/b] 3r33333. 3r3638. private function onWorkerStart (swoole_websocket_server $ ws)
$ this-> messagesRepository = new MessagesRepository (); 3r3755. 3r3755. $ ws-> tick (self :: PING_DELAY_MS, function () use ($ ws) {
foreach ($ ws-> connections as $ id) {
$ ws-> push ($ id, 'ping', WEBSOCKET_OPCODE_PING ); 3r3755.}
}); 3r3755.}
3r3741.  3r3755. 3r3751. 3r3751. 3r3741.  3r3755. Next, I implemented a simple feature for pinging a MySQL server every N seconds using swooleTimer:
 3r3755. 3r3741.  3r3755. 3r3630. 3r3633. DatabaseHelper 3r3-33632. 3r33333. The timer itself starts in initPdo if not already on:
 3r3755. 3r3741.  3r3755. 3r3638. /**
* Init new Connection, and ping DB timer function 3r3755. * /
private static function initPdo ()
if (self :: $ timerId === null || (! Timer :: exists (self :: $ timerId))) {
self :: $ timerId = Timer :: tick (self :: MySQL_PING_INTERVAL, function () {
self :: ping ();
}); 3r3755.}
3r3755. self :: $ pdo = new PDO (self :: DSN, DBConfig :: USER, DBConfig :: PASSWORD, self :: OPT); 3r3755.}
3r3755. /**
* Ping database to maintain the connection
* /
private static function ping ()
try {3r3755. self :: $ pdo-> query ('SELECT 1');
} catch (PDOException $ e) {
self :: initPdo (); 3r3755.}
3r3670. 3r3741.  3r3755. 3r3751. 3r3751. 3r3741.  3r3755. The main part of the work was to write logic for adding, saving, sending messages (no more difficult than the usual CRUD), and then a huge scope for improvements. 3r3741.  3r3755. 3r3741.  3r3755. So far, I have brought my code to a more or less readable form and object-oriented style, I implemented a bit of functionality:
 3r3755. 3r3741.  3r3755. - Login by name; 3r3741.  3r3755. 3r3741.  3r3755. 3r3630. 3r3633. - Verify that the name is not occupied [/b] 3r33333. 3r3638. /**
* @param string $ username
* @return bool
* /
private function isUsernameCurrentlyTaken (string $ username) {
foreach ($ this-> usersRepository-> getByIds ($ this-> ws-> connection_list ()) as $ user) {
if ($ user-> getUsername () == $ username) {
return true; 3r3755.}
return false; 3r3755.}
3r3741.  3r3755. 3r3751. 3r3751. 3r3741.  3r3755. 3r3630. 3r3633. - The limiter requests for protection against spam [/b] 3r33333. 3r3638. 3r3640. 3r3755. namespace AppHelpers; 3r3755. 3r3755. use SwooleChannel; 3r3755. 3r3755. class RequestLimiter
* @var Channel
* /
private $ userIds; 3r3755. 3r3755. const MAX_RECORDS_COUNT = 10; 3r3755. 3r3755. const MAX_REQUESTS_BY_USER = 4; 3r3755. 3r3755. public function __construct () {
$ this-> userIds = new Channel (1024 * 64); 3r3755.}
3r3755. /**
* Check if there are
* and make a record of request from that user
* @param int $ userId
* @return bool
* /
public function checkIsRequestAllowed (int $ userId) {
$ requestsCount = $ this-> getRequestsCountByUser ($ userId); 3r3755. $ this-> addRecord ($ userId); 3r3755. if ($ requestsCount> = self :: MAX_REQUESTS_BY_USER) return false; 3r3755. return true; 3r3755.}
3r3755. /**
* @param int $ userId
* @return int
* /
private function getRequestsCountByUser (int $ userId) {
$ channelRecordsCount = $ this-> userIds-> stats ()['queue_num']; 3r3755. $ requestsCount = 0; 3r3755. 3r3755. for ($ i = 0; $ i < $channelRecordsCount; $i++) {
$ userIdFromChannel = $ this-> userIds-> pop ();
$ this-> userIds-> push ($ userIdFromChannel);
if ($ userIdFromChannel === $ userId) {
$ requestsCount ++;
return $ requestsCount; 3rr.3755.}
int $ userId) {
$ recordsCount = $ this-> userIds-> stats ()['queue_num'];
if ($ recordsCount> = self :: MAX_RECORDS_COUNT) {
$ This-> userIds-> push ($ userId);
} 3r37070. 3r3671.
 3r3755. P.S .: Yes, the check is on connection id. Perhaps it makes sense to replace it in this case, for example, with the user's IP address. 3r3741.  3r3755. 3r3741.  3r3755. I'm also not sure that swoole_channel was best suited in this situation. I think later to reconsider this point. 3r3741.  3r3755. 3r3751. 3r3751. 3r3741.  3r3755. - Simple protection against XSS using 3r33624. ezyang /htmlpurifier 3r3741.  3r3755. 3r3741.  3r3755. 3r3630. 3r3633. - A simple spam filter [/b] 3r33333. With the ability to further add additional checks. 3r3741.  3r3755. 3r3741.  3r3755. 3r3638. 3r3640. 3r3755. namespace AppHelpers; 3r3755. 3r3755. class SpamFilter
* @var string[]errors
* /
private $ errors =[]; 3r3755. 3r3755. /**
* @param string $ text
* @return bool
* /
public function checkIsMessageTextCorrect (string $ text) {
$ isCorrect = true; 3r3755. if (empty (trim ($ text))) {
$ this-> errors[]= 'Empty message text'; 3r3755. $ isCorrect = false; 3r3755.}
return $ isCorrect; 3r3755.}
3r3755. /**
* @return string[]errors
* /
public function getErrors (): array {
return $ this-> errors; 3r3755.}

3r3741.  3r3755. 3r3751. 3r3751. 3r3741.  3r3755. Frontend in the chat is still very raw, because I am more attracted to the backend, but when there is more time I will try to make it more pleasant. 3r3741.  3r3755. 3r3741.  3r3755. 3r38282. Where to get information, find out news about the framework?
3r3741.  3r3755.
 3r3755. 3r33737. English official website 3r33737. - useful links, up-to-date documentation, some comments from users of 3r33716.  3r3755. 3r33737. 3r3694. Twitter - current news, useful links, interesting articles
 3r3755. 3r33737. 3r3699. Issue tracker (Github)
- bugs, questions, communication with the creators of the framework. They answer very quickly (my question was answered with a question in a couple of hours, helped with the implementation of pingloop). 3r33737.  3r3755. 3r33737. 3r3704. Closed issues
- I also advise. A large database of questions from users and answers from the creators of the framework. 3r33737.  3r3755. 3r33737. Tests written by developers - practically on each module from the documentation there are tests written in PHP, showing use cases. 3r33737.  3r3755. 3r33737. 3r33714. Chinese wiki framework
- All the information is in English, but much more comments from users (Google translator to help). 3r33737.  3r3755. 3r33737. 3r3741.  3r3755. 3r33737. API documentation 3r33737. - A description of some classes and functions of the framework in a fairly convenient form. 3r3741.  3r3755. 3r3741.  3r3755.


3r3741.  3r3755. It seems to me that Swoole was very actively developing over the past year, out of the stage when it could be called “raw”, and now it is in competition with the use of node.js /go from the point of view of asynchronous programming and implementation of network protocols. 3r3741.  3r3755. 3r3741.  3r3755. I would be happy to hear various opinions on the topic and feedback from those who already have experience using Swoole
 3r3755. 3r3741.  3r3755. You can chat in the described chatika on link 3r3741.  3r3755. Sources are available at Github . 3r3751. 3r3755. 3r3755. 3r33748. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e. ): d ()}}} t ("//"""_mediator") () (); 3r33737. 3r3755. 3r3751. 3r3755. 3r3755. 3r3755. 3r3755.
+ +1 -

Add comment