Skip to content

Instantly share code, notes, and snippets.

@ptcampbell
Last active June 21, 2021 03:22
Show Gist options
  • Save ptcampbell/4f9dad9ebd539e0357e279ee75663ee8 to your computer and use it in GitHub Desktop.
Save ptcampbell/4f9dad9ebd539e0357e279ee75663ee8 to your computer and use it in GitHub Desktop.
declare global {
interface Window {
Pusher: any
}
}
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js')
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'ad6bcb7780f189a3746e',
cluster: 'us3',
forceTLS: true,
})
const channel = window.Echo.channel('toast-channel')
channel.listen('.toast-event', function(data) {
alert(JSON.stringify(data))
})
<?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 ToastMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('toast-channel');
// return new PrivateChannel('toast.' . $this->user);
}
public function broadcastAs()
{
return 'toast-message';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment