PHP code example of pupper / pupper-php

1. Go to this page and download the library: Download pupper/pupper-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pupper / pupper-php example snippets


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);

use Pupper\Pupper\Event;

$websocket = (new Pupper\Pupper\WebSocket)
    ->addEventListener('custom', function (Event $event) {
        return (new Event)
            ->setName('custom')
            ->setValue('From PHP: ' . $event->getValue());
    });

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!'
        );
    });

$websocket = (new \Pupper\WebSocket)->allowOrigin('https', 'your.domain.com', 80);

use Pupper\Pupper\Event;

function (Event $event) {
    echo $event->getName();
    echo $event->getValue();
});

use Pupper\Pupper\Event;

$event = (new Event)
    ->setName('hello_event')
    ->setValue('Hello from PHP!');

use Pupper\Pupper\Event;

$event = new Event(
    'hello_event', 
    'Hello from PHP!'
);