PHP code example of codemash / laravel-socket

1. Go to this page and download the library: Download codemash/laravel-socket 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/ */

    

codemash / laravel-socket example snippets


'providers' => [
    ...
    Codemash\Socket\SocketServiceProvider::class,
],

'aliases' => [
    ...
    'Socket' => Codemash\Socket\Facades\Socket::class,
]



namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
   ...

    /**
    * The subscriber classes to register.
    *
    * @var array
    */
    protected $subscribe = [
        'App\Listeners\MessageEventListener'
    ];
}



namespace App\Listeners;

use Codemash\Socket\Events\MessageReceived;
use Codemash\Socket\Events\ClientConnected;
use Codemash\Socket\Events\ClientDisconnected;

class MessageEventListener {

    public function onMessageReceived(MessageReceived $event)
    {
        $message = $event->message;

        // If the incomming command is 'sendMessageToOthers', forward the message to the others.
        if ($message->command === 'sendMessageToOthers') {
            // To get the client sending this message, use the $event->from property.
            // To get a list of all connected clients, use the $event->clients pointer.
            $others = $event->allOtherClients();
            foreach ($others as $client) {
                // The $message->data property holds the actual message
                $client->send('newMessage', $message->data);
            }
        }
    }

    public function onConnected(ClientConnected $event)
    {
        // Not used in this example.
    }

    public function onDisconnected(ClientDisconnected $event)
    {
        // Not used in this example.
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param  Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            'Codemash\Socket\Events\ClientConnected',
            'App\Listeners\MessageEventListener@onConnected'
        );

        $events->listen(
            'Codemash\Socket\Events\MessageReceived',
            'App\Listeners\MessageEventListener@onMessageReceived'
        );

        $events->listen(
            'Codemash\Socket\Events\ClientDisconnected',
            'App\Listeners\MessageEventListener@onDisconnected'
        );

    }
}

foreach ($clients as $client) {
    if ($client->authed()) {
        $user = $client->getUser();
        // $user now holds the App\User model,
        // or the model set in the 'config.auth.providers.users.model' config variable.
    }
}

public function onConnected(ClientConnected $event)
{
    $event->client->connected_at = Carbon::now();
}

public function onMessageReceived(MessageReceived $event)
{
    if ($event->message->command === 'whenDidIConnect') {
        if (isset($event->from->connected_at)) {
            $event->from->send('iConnectedAt', $event->from->connected_at->toString());
        }
    }
}
sh
php artisan vendor:publish --provider="Codemash\Socket\SocketServiceProvider"
sh
php artisan socket:listen
sh
nohup php artisan socket:listen &