人気のPHP WEBアプリケーションフレームワークLaravelのTipsを記録していきます

Laravel Echo Server のインストール

● Laravel Echo Server のインストール

* 1. laravel-echo のインストール

npm install --save laravel-echo socket.io-client

* 2. laravel-echo-server のインストール

npm install -g laravel-echo-server

SSL鍵ファイルの場所を調べておく(nginx の場合)

cat /etc/nginx/conf.d/[YOUR-DOMAIN-NAME]_ssl.conf | grep cert

初期化 (Laravelのプロジェクトトップに移動してから初期化します)

cd [Laravelのプロジェクトトップ]
laravel-echo-server init

次のように質問に答えます( YOUR-SERVER.COM は適宜読み替えてください。 )

? Do you want to run this server in development mode? Yes
? Which port would you like to serve from? 6001
? Which database would you like to use to store presence channel members? redis
? Enter the host of your Laravel authentication server. https://YOUR-SERVER.COM/
? Will you be serving on http or https? https
? Enter the path to your SSL cert file. /etc/letsencrypt/live/YOUR-SERVER.COM/fullchain.pem
? Enter the path to your SSL key file. /etc/letsencrypt/live/YOUR-SERVER.COM/privkey.pem
? Do you want to generate a client ID/Key for HTTP API? No
? Do you want to setup cross domain access to the API? No
? What do you want this config to be saved as? laravel-echo-server.json

完了すると laravel-echo-server.json が作成されます。

* 3. 6001 ポートを開ける

サーバーの6001ポートのファイアーウォールを空けておきます。

* 4. redis のインストール

sudo yum install -y epel-release
sudo yum install -y redis

redisのバージョン確認

redis-server --version

redisの起動

redis-server

● そのまま起動すると ssl証明書のエラーが出るので次のコマンドを実行します

/opt/eff.org/certbot/venv/bin/letsencrypt certonly -a webroot --webroot-path=[site document root] --agree-tos --email [your email] --config-dir="/home/[user]/letsencrypt/etc" --domains [site domain]

https://komelin.com/articles/realtime-apps-laravel-echo-tips-and-tricks/#laravel-echo-server-through-https

● redisサーバーの起動

redis-server

● laravel-echo-server サーバーの起動

laravel-echo-server init
laravel-echo-server start

● Laravelアプリの設定

1. config/app.php の providers のコメントアウトをやめる

App\Providers\BroadcastServiceProvider::class,      // ON

2. .env の変更

BROADCAST_DRIVER=log

 ↓ 変更

BROADCAST_DRIVER=redis

● Laravelアプリでイベントの作成

1. イベントの作成

php artisan make:event PublicEvent

app/Events/PublicEvent.php が自動生成されますので以下の内容で保存します。

app/Events/PublicEvent.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PublicEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct()
    {
    }

    public function broadcastOn()
    {
        return new Channel('public-event');
    }

    public function broadcastWith()
    {
        return [
            'message' => 'PUBLIC',
        ];
    }
}

2. ルーティングの追加

routes/web.php

Route::get('/test-public-event', function(){
    broadcast(new \App\Events\PublicEvent);
    return 'test-public-event';
});

3. resources/js/bootstrap.js に記述

import Echo from "laravel-echo"
window.io = require('socket.io-client')
window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://' + window.location.hostname + ':6001'
})
//購読するチャネルの設定
window.Echo.channel('public-event')
            .listen('PublicEvent', (e) => {
                console.log(e);
            });

4. npm のビルド

npm install
npm run dev
No.1744
04/26 23:27

edit