Simple websocket implementation in laravel 5

后端 未结 1 716
别那么骄傲
别那么骄傲 2020-12-24 08:07

I need to implement very simple and very basic websocket in Laravel to implement data synchronization process between my phonegap app as client and my Laravel Website as ser

相关标签:
1条回答
  • 2020-12-24 08:42

    I ended up using brainboxlabs's brainsocket (https://github.com/BrainBoxLabs/brain-socket) . As its document says its laravel 4 package but it also works with laravel 5 without any issue.

    To install this package with laravel 5. Follow the documentation on the above github link. Where it says to create an event.php file in app folder and some events related code. Instead of this step simply add that event related code in app/Providers/EventServiceProvider.php file. In its boot method, add that code which is

    Event::listen('generic.event',function($client_data){
        return BrainSocket::message('generic.event',array('message'=>'A message from a generic event fired in Laravel!'));
    });
    
    Event::listen('app.success',function($client_data){
        return BrainSocket::success(array('There was a Laravel App Success Event!'));
    });
    
    Event::listen('app.error',function($client_data){
        return BrainSocket::error(array('There was a Laravel App Error!'));
    });
    

    After this step there was a step of adding

    require app_path().'/filters.php';
    require app_path().'/events.php';
    

    in app/start/global.php . You can leave this step for laravel 5.

    Ok so Web socket has been implemented. You can test by starting the websocket server using cmd by running the command artisan brainsocket:start. You can optionally provide it the port artisan brainsocket:start 9000

    Another requirement was to call controller to to perform rest of the task. For this i directly edited into to provider package. I do not recommend this as it is not a good way. When you will update your package using composer your changes will be lost. So you have to find a better option. But its just a one line change.

    In vendor\brainboxlabs\brain-socket\src\BrainSocket\BrainSocketServer.php i edited code in method "start" and replace

    $this->server = IoServer::factory(
                new HttpServer(
                    new WsServer(
                        new BrainSocketEventListener(
                            new BrainSocketResponse(new LaravelEventPublisher())
                        )
                    )
                )
                , $port
            );
    

    with

    $this->server = IoServer::factory(
                new HttpServer(
                    new WsServer(
                   new \FMIS\Http\Controllers\SynchronizationController(
                      new BrainSocketResponse(new LaravelEventPublisher())
                                                )
                    )
                )
                , $port
            );
    

    And in my SynchronizationController file.

    I added this on top

    use Ratchet\MessageComponentInterface;
    use Ratchet\ConnectionInterface;
    use BrainSocket\BrainSocketResponseInterface;
    

    Implemented interface like this.

    class SynchronizationController extends Controller implements MessageComponentInterface{
    

    and implemented the methods of this interface.

    public function __construct(BrainSocketResponseInterface $response) {
            $this->clients = new \SplObjectStorage;
            $this->response = $response;
    }
    
    public function onOpen(ConnectionInterface $conn) {
            echo "Connection Established! \n";
    }
    
    
    public function onMessage(ConnectionInterface $conn, $msg){
     echo "this messge gets called whenever there is a messge sent from js client";
    }
    
    public function onClose(ConnectionInterface $conn) {
        echo "Connection {$conn->resourceId} has disconnected\n";
    }
    
    public function onError(ConnectionInterface $conn, \Exception $e) {
            $msg = "An error has occurred: {$e->getMessage()}\n";
            echo $msg;
            $conn->close();
    }
    

    You have to change in these methods to implement your functionality. After this you can call from your js client. And you are not required to use its js library as well. You simply send data using js client describe in this tutorial http://www.binarytides.com/websockets-php-tutorial/ .

    Let me know if somebody need any more help regarding its implementation.

    0 讨论(0)
提交回复
热议问题