Код: Выделить всё
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST ?? window.location.hostname,
wsPort: import.meta.env.VITE_REVERB_PORT ?? 8080,
wssPort: import.meta.env.VITE_REVERB_PORT ?? 8080,
forceTLS: false,
enabledTransports: ['ws'],
});
window.Echo.channel('chat')
.listen('MessageSent', (e) => {
console.log('Received:', e.message);
});
< /code>
Chat Test
@vite('resources/js/app.js')
Open console and trigger /send
< /code>
I ran php artisan reverb:startКогда я доступ к маршруту/отправлю, я транслирую это:
Route::get('/send', function () {
broadcast(new MessageSent('Hello World'));
\Log::info('MessageSent fired');
return 'sent';
});
< /code>
And the Event looks like that :
class MessageSent implements ShouldBroadcast
{
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new Channel('chat');
}
}
< /code>
and my .env have those :
BROADCAST_CONNECTION=reverb
REVERB_APP_ID=app-id
REVERB_APP_KEY=app-key
REVERB_APP_SECRET=app-secret
REVERB_HOST=127.0.0.1
REVERB_PORT=8080
REVERB_SCHEME=http
VITE_REVERB_APP_KEY=app-key
VITE_REVERB_HOST=127.0.0.1
VITE_REVERB_PORT=8080
< /code>
Where did I go wrong?
To respond to people, yes I have created a channel, (I let the default one)
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
< /code>
When I run through --debug, I see two pings
1▕ {
2▕ "event": "pusher:ping",
3▕ "data": []
4▕ }
< /code>
And
1▕ {
2▕ "event": "pusher:subscribe",
3▕ "data": {
4▕ "auth": "",
5▕ "channel": "chat"
6▕ }
7▕ }
< /code>
However, still no sign of log in the console of /chat. And I never see any warnings or erros.
NEWS : Apparently typing queue:work made them all fire at once ... At least it worked, no idea why though. Are you supposed to run
php artisan queue:work
< /code>
to work ?
To answer for future people, yes I suppose you are kinda required to run queue:work for the event to be handled.
Подробнее здесь: https://stackoverflow.com/questions/797 ... vel-reverb
Мобильная версия