참고로 라라벨 에코서버를 이용할 때는 redis서버가 반드시 필요하기 때문에 사전작업의 redis서버를 준비해 주세요
그 후 라라벨 에코 서버 json파일을 작성해줍니다.
laravel-echo-server init
? 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. http://localhost:8000
? Will you be serving on http or https? http
? Do you want to generate a client ID/Key for HTTP API? Yes
? Do you want to setup cross domain access to the API? Yes
? Specify the URI that may access the API: http://localhost:8000
? Enter the HTTP methods that are allowed for CORS: GET, POST
? Enter the HTTP headers that are allowed for CORS: Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Autho
rization, X-CSRF-TOKEN, X-Socket-Id
? What do you want this config to be saved as? laravel-echo-server.json
appId: fb9e72802cace662
key: 9bf6eb178b5601e2353d387c9d46531b
init을 이용하면 위와 같이 설정을 할 수 있습니다.
마지막으로 라라벨 에코 서버를(laravel-echo-server start) 기동시 킵니다.
$laravel-echo-server start
L A R A V E L E C H O S E R V E R
version 1.5.3
Starting server...
✔ Running at localhost on port 6001
✔ Listening for http events...
✔ Listening for redis events...
Server ready!
혹시 아래와 같은 에러가 보인다면 redis서버가 준비되어있지 않은 상태이니 한번 확인해주세요!
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class PublicEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
// redis채널이름
return new Channel('public-event');
}
public function broadcastWith()
{
return [
"message" => "hello world",
];
}
}