/laravel-phpsocket.io

Laravel integrated phpsocket.io

Primary LanguagePHPMIT LicenseMIT

Build Status codecov Maintainability StyleCI Quality Score

Laravel with phpsocket.io

This is Laravel package for phpsocket.io which is a server side alternative implementation of socket.io in PHP based on Workerman.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Installing

Require this package, with Composer, in the root directory of your project.

$ composer require sanchescom/laravel-phpsocket.io

Create SocketServiceProvider.php in app/Providers

<?php

namespace App\Providers;

use Sanchescom\LaravelSocketIO\SocketServiceProvider as ServiceProvider;
use App\Sockets\ExampleSocket;

/**
 * Class SocketServiceProvider.
 */
class SocketServiceProvider extends ServiceProvider
{
    /**
     * The socket handlers for the application.
     *
     * @var array
     */
    protected $sockets = [
        ExampleSocket::class,
    ];
}

Laravel 5.x:

After updating composer, add the ServiceProvider to the providers array in config/app.php

'providers' => [
   ...
   App\Providers\SocketServiceProvider::class,
],

Lumen:

After updating composer add the following lines to register provider in bootstrap/app.php

$app->register(App\Providers\SocketServiceProvider::class);

Usage

Create folder app\Sockets and put there ExampleSocket.php

<?php

namespace App\Sockets;

use PHPSocketIO\SocketIO;
use Sanchescom\LaravelSocketIO\Sockets\AbstractSocket;
use Workerman\Lib\Timer;

class ExampleSocket extends AbstractSocket
{
    /**
     * @var int
     */
    const TIME_INTERVAL = 4;

    /**
     * @var int
     */
    protected $port = 2020;

    /**
     * @var array
     */
    protected $options = [];

    /**
     * @param SocketIO $socketIO
     */
    public function call(SocketIO $socketIO): void
    {
        $socketIO->on('workerStart', function () use ($socketIO) {
            Timer::add(self::TIME_INTERVAL, function () use ($socketIO) {
                $socketIO->to('room')->emit('score', [
                    'items' => [
                        [
                            'id' => 1,
                            'message' => 'Hello world'
                        ],
                    ]
                ]);
            });
        });

        $socketIO->on('connection', function ($socket) {
            $socket->join('room');
        });
    }
}

Running

Start

$ ./vendor/bin/socket start

Stop:

$ ./vendor/bin/socket stop

Status

$ ./vendor/bin/socket status

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details