Mastering Swoole PHP
讀書會#13
07/01
19:30~21:00
@Google Meet
Websocket
Websocket
Websocket
-
Stateful Protocol
-
Full Duplex
-
Bi-directional Messaging Pattern
-
Long-lived TCP Connection
-
Widely Supported by Modern Browsers
-
More Efficient Than HTTP Polling
Websocket
GET / HTTP/1.1
Host: www.cnn.com
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.68 Safari/537.17
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: [[[2428 byte of cookie data]]]
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 13 Feb 2013 18:56:27 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: CG=US:TX:Arlington; path=/
Last-Modified: Wed, 13 Feb 2013 18:55:22 GMT
Vary: Accept-Encoding
Cache-Control: max-age=60, private
Expires: Wed, 13 Feb 2013 18:56:54 GMT
Content-Encoding: gzip
-
HTTP Header Payload
Websocket
-
Websocket Payload
Websocket
-
Websocket Server
<?php
$server = new Swoole\WebSocket\Server('0.0.0.0', 9501);
$server->on('open', function ($server, $request) {
echo "handshake succeeded with fd {$request->fd}\n";
});
$server->on('message', function ($server, $frame) {
if ($frame->data === 'exit') {
$server->disconnect($frame->fd);
return;
}
echo "received from {$frame->fd}:{$frame->data}, opcode:{$frame->opcode}, fin:{$frame->finish}\n";
$server->push($frame->fd, $frame->data);
});
$server->on('close', function ($server, $fd) {
echo "client {$fd} closed\n";
});
echo "Server is starting...\n";
$server->start();
Websocket
When is HTTP better?
When is Websocket Better?
Websocket
-
When is HTTP better?
- Retrieve Resource
- Highly Cacheable Resource
- Error Scenarios
- Request-response Pattern
Websocket
-
When is Websocket better?
- Fast Reaction Time
- Real Time Data Updates
- Ad-hoc Messaging
- High-Frequency Messaging with Small Payloads
Websocket
Is Websocket Suitable
for Large Payload Transfer?
Websocket
-
Websocket Transfer Protocol
- Protocol itself basically doesn't have a size limit. (2^63)
- Websocket is a Frame based protocol.
- Chunking in Application Layer is required for multiplexing.
- A size limit is always defined in many implementations or SaaS platforms.
- Large payload in websocket transfer in not efficient.
Websocket
How to Get Unreceived Messages
When Clients Are Disconnected?
Websocket
How to Get Unreceived Messages
When Clients Are Disconnected?
Websocket
How to Scale Your Websocket Server?
Websocket
Discussion
Copy of Mstering Swoole PHP 讀書會#13
By Albert Chen
Copy of Mstering Swoole PHP 讀書會#13
- 219