Richard Melo
@allucardster
"It's a computer communication protocol that enables interaction between a browser and a web server"
Handshake
Connection opened
Bi-directional messages
Open and persistent connection
One side closes the connection
Connection closed
Time
"Ratchet is a PHP library to create real time, bi-directional applications between clients and servers over WebSockets."
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn)
{
//...
}
public function onMessage(ConnectionInterface $from, $msg)
{
//...
}
public function onClose(ConnectionInterface $conn)
{
//...
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
//...
}
}<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat() // WebSocket handler instance
)
),
8080
);
$server->run();:~$ php bin/chat-server.phpHow to run it?
var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
};conn.send('Hello World!');How to send a message?
"Gos Web Socket is a bundle designed to integrate the WebSocket functionality on a Symfony application"
https://github.com/allucardster/ws-xample