npm install --save laravel-echo socket.io-client
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 が作成されます。
サーバーの6001ポートのファイアーウォールを空けておきます。
sudo yum install -y epel-release
sudo yum install -y redis
redisのバージョン確認
redis-server --version
redisの起動
redis-server
/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]
redis-server
laravel-echo-server init
laravel-echo-server start
App\Providers\BroadcastServiceProvider::class, // ON
BROADCAST_DRIVER=log
↓ 変更
BROADCAST_DRIVER=redis
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',
];
}
}
routes/web.php
Route::get('/test-public-event', function(){
broadcast(new \App\Events\PublicEvent);
return 'test-public-event';
});
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);
});
npm install
npm run dev