, (*1)
, (*2)
Pupper stands for "PHP Plus React" (PPR > Pupper). The goal is to make a Framework that takes the best of both technologies and makes them communicate bi-directionnaly., (*3)
See an example implementation, (*4)
Quick start
Pupper PHP is based on Aerys, a non-blocking PHP application and Websocket framework., (*5)
Here is a quick overview of the code to get started., (*6)
use Pupper\Pupper\Event;
// Initiates WebSocket connection
$websocket = (new Pupper\Pupper\WebSocket)
// Filter allowed clients (optional)
->allowOrigin('https', 'your.domain.com', 443);
// Defines a callback for 'my_event'
->addEventListener('my_event', function (Event $event) {
// Dispatches to all clients
$websocket->broadcastEvent(
new Event('notify_all', 'Something has happened!');
);
// Dispatches to the client that triggered the callback
return (new Event)
->setName('operation_done')
->setValue('Your value was ' . $event->getValue());
});
$router = Aerys\router()->route('GET', '/', Aerys\websocket($websocket));
// Exposes the websocket to the 1337 port
return (new Aerys\Host)->use($router)->expose('*', 1337);
API
WebSocket
WebSocket is the class that initiates the WebSocket on the PHP side., (*7)
addListener, (*8)
addListener takes the event name as first parameter, and a callback function as a second parameter., (*9)
If you return an Event, it will be dispatched to the client that triggered the callback., (*10)
use Pupper\Pupper\Event;
$websocket = (new Pupper\Pupper\WebSocket)
->addEventListener('custom', function (Event $event) {
return (new Event)
->setName('custom')
->setValue('From PHP: ' . $event->getValue());
});
broadcastEvent, (*11)
broadcastEvent dispatches an event to all the clients., (*12)
use Pupper\Pupper\Event;
$websocket = (new Pupper\Pupper\WebSocket)
->addEventListener('player_has_joined', function (Event $event) {
$websocket->broadcastEvent(
'player_count_updated',
'A new player has joined!'
);
});
Client filtering, (*13)
Set WebSocket's constructor's protocol, host and port parameters to restrict the access to your websocket ., (*14)
$websocket = (new \Pupper\WebSocket)->allowOrigin('https', 'your.domain.com', 80);
Event
Event represents an event from the PHP side., (*15)
Read, (*16)
Event has getName() and getValue() methods to read the event's name and value., (*17)
use Pupper\Pupper\Event;
function (Event $event) {
echo $event->getName();
echo $event->getValue();
});
Write, (*18)
Event has setName() and setValue() methods to write the event's name and value., (*19)
use Pupper\Pupper\Event;
$event = (new Event)
->setName('hello_event')
->setValue('Hello from PHP!');
Construct, (*20)
Event's constructor also accepts the event's name and value as parameters., (*21)
use Pupper\Pupper\Event;
$event = new Event(
'hello_event',
'Hello from PHP!'
);
Credits
License
Unlicense. Please see License File for more information., (*22)