Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pupper/pupper-php
PHP components for Pupper
https://github.com/pupper/pupper-php
php pupper react websockets
Last synced: 3 months ago
JSON representation
PHP components for Pupper
- Host: GitHub
- URL: https://github.com/pupper/pupper-php
- Owner: pupper
- License: unlicense
- Archived: true
- Created: 2017-10-17T15:37:37.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-20T08:36:24.000Z (about 7 years ago)
- Last Synced: 2024-10-11T09:25:54.236Z (4 months ago)
- Topics: php, pupper, react, websockets
- Language: PHP
- Homepage: https://pupper.github.io/pupper-php
- Size: 28.3 KB
- Stars: 14
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README

[](https://www.codacy.com/app/bouiboui/pupper-php?utm_source=github.com&utm_medium=referral&utm_content=pupper/pupper-php&utm_campaign=badger)
![Software License][ico-license] [](https://insight.sensiolabs.com/projects/38ef8a9c-deb5-41d4-a3f3-432fa411e921)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.
[See an example implementation](https://github.com/bouiboui/pupper/tree/master/app)
## Quick start
Pupper PHP is based on [Aerys](https://amphp.org/aerys/), a non-blocking PHP application and Websocket framework.
Here is a quick overview of the code to get started.
```php
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.
**addListener**
`addListener` takes the event name as first parameter, and a callback function as a second parameter.
If you `return` an `Event`, it will be dispatched to the client that triggered the callback.
```php
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**
`broadcastEvent` dispatches an event to all the clients.
```php
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**
Set WebSocket's constructor's protocol, host and port parameters to restrict the access to your websocket .
```php
$websocket = (new \Pupper\WebSocket)->allowOrigin('https', 'your.domain.com', 80);
```### Event
`Event` represents an event from the PHP side.
**Read**
`Event` has `getName()` and `getValue()` methods to read the event's name and value.
```php
use Pupper\Pupper\Event;function (Event $event) {
echo $event->getName();
echo $event->getValue();
});
```**Write**
`Event` has `setName()` and `setValue()` methods to write the event's name and value.
```php
use Pupper\Pupper\Event;$event = (new Event)
->setName('hello_event')
->setValue('Hello from PHP!');
```**Construct**
`Event`'s constructor also accepts the event's name and value as parameters.
```php
use Pupper\Pupper\Event;$event = new Event(
'hello_event',
'Hello from PHP!'
);
```## Credits
- [bouiboui][link-author]
- [All Contributors][link-contributors]## License
Unlicense. Please see [License File](LICENSE.md) for more information.
[ico-license]: https://img.shields.io/badge/license-Unlicense-brightgreen.svg?style=flat-square
[link-author]: https://github.com/bouiboui
[link-contributors]: ../../contributors