websockets with php: mission impossible
TRANSCRIPT
![Page 1: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/1.jpg)
WebSockets with PHP:Mission Impossible
![Page 2: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/2.jpg)
Who am I?
✔ Yoan-Alexander Grigorov
✔ Proud PHP developer
✔ Likes to screw around with OOP
![Page 3: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/3.jpg)
So what is a WebSocket after all?
![Page 4: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/4.jpg)
![Page 5: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/5.jpg)
WebSocket boosts UX
✔ Forget client pulls
✔ Forget long polling
✔ Make your application event based
![Page 6: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/6.jpg)
What google say about WebSockets:
✔ It's easy to build WebSocket server with node.js
✔ It's hard to build WebSocket server with PHP
![Page 7: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/7.jpg)
Research! Research! Research!
![Page 8: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/8.jpg)
Available tools for PHP
✔ Ratchet
✔ … it's only that. Seriously, it's only Ratchet
✔ Ok, there are some other libraries, all of them named PHPWS
![Page 9: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/9.jpg)
Ratchethttp://socketo.me
![Page 10: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/10.jpg)
First things first
![Page 11: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/11.jpg)
Install with composer
![Page 12: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/12.jpg)
Setting up a server with Ratchet<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Handler 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) {}
}
![Page 13: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/13.jpg)
Setting up a server with Ratchet<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
$handler = new App\Handler();
$server = IoServer::factory(
new HttpServer(new WsServer(
$handler
)
), 8123);
$server->run();
![Page 14: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/14.jpg)
In the browservar ws = new WebSocket('ws://127.0.0.1:8123');
// ... in some event
ws.send('Гледай кви ръки, чуек!');
// ... listener for messages:
ws.onmessage = function (message) {
alert(message);
};
![Page 15: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/15.jpg)
Let's make a chat!
![Page 16: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/16.jpg)
What do we need?
✔ Keep all active connection objects
✔ When a connection sends a message, notify all others
✔ On disconnect – remove connection object from the in-memory cache (map)
![Page 17: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/17.jpg)
Connections handling<?php
class Handler implements MessageComponentInterface {
private $connections = [];
public function onOpen(ConnectionInterface $conn) {
$this->connections[$conn->resourceId] = $conn;
}
public function onMessage(ConnectionInterface $from, $msg) {
foreach ($this->connections as $receiver) {
$receiver->send($msg);
}
}
public function onClose(ConnectionInterface $conn) {
unset($this->connections[$conn->resourceId]);
}
}
![Page 18: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/18.jpg)
That's it!
![Page 19: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/19.jpg)
Keep an eye on:
✔ Run it like a daemon (/etc/init.d/skeleton)
✔ Fatal errors and exceptions – they kill the daemon
✔ ORMs like Doctrine help you a lot
![Page 20: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/20.jpg)
Doctrine uses Identity Map
![Page 21: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/21.jpg)
![Page 22: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/22.jpg)
Identity map gives you wings!
![Page 23: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/23.jpg)
Web Servers and Web Sockets
![Page 24: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/24.jpg)
Don't forget good old Ajax!
![Page 25: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/25.jpg)
But how ajax could send messages to the WebSocket server?
![Page 26: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/26.jpg)
Message Queues
![Page 27: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/27.jpg)
![Page 28: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/28.jpg)
ZeroMQ
✔ Libzmq
✔ Has a PHP extension in PECL
✔ React/ZMQ (PHP wrapper)
✔ "react/zmq": "0.2.*|0.3.*" in Composer
![Page 29: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/29.jpg)
What you need to know
✔ There are ZeroMQ listeners– They use simple TCP
✔ There are ZeroMQ contexts (senders)– They connect to ZeroMQ listeners and send
messages
![Page 30: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/30.jpg)
What you need to know
✔ You need ZeroMQ listener in the WebSocket daemon
✔ Ordinary PHP scripts send messages to the listener
✔ Since ZeroMQ is in the WebSocket daemon, it could easily interact with WebSocket connections
![Page 31: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/31.jpg)
Let's add ZeroMQ to our application!
![Page 32: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/32.jpg)
Add callback for ZeroMQ messages<?php
class Handler implements MessageComponentInterface
{
// ...
// this is CUSTOM method (not from interface)
public function onZMQMessage($message)
{
}
}
![Page 33: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/33.jpg)
Add ZeroMQ listener to the daemon<?php
use ...
$handler = new App\Handler();
$server = IoServer::factory(
new HttpServer(new WsServer(
$handler
)
), 8123);
// Listen on 127.0.0.1:4444
$context = new \React\ZMQ\Context($server->loop);
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
$pull->bind("tcp://127.0.0.1:4444");
$pull->on('message', function ($data) use ($handler) {
$handler->onZMQMessage($data);
});
$server->run();
![Page 34: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/34.jpg)
In your Ajax server script<?php
// ... add stuff to the database, other things...
// notify using ZeroMQ:
$context = new \ZMQContext();
$socket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'pusher');
$socket->connect("tcp://127.0.0.1:4444");
$socket->send('Глей кви ръки! Туй аз ли съм!?');
![Page 35: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/35.jpg)
Go! Try it yourself!
![Page 36: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/36.jpg)
Working code examples from this talk:
https://github.com/sasquatch-mc/ratchet-example(check the branches)
![Page 37: WebSockets with PHP: Mission impossible](https://reader033.vdocuments.net/reader033/viewer/2022042701/55a5be671a28ab4c6b8b460f/html5/thumbnails/37.jpg)
References
✔ http://socketo.me
✔ http://zeromq.org