What do I need? I need when a user in the frontend sends a message to the system and the dashboard backend receives a real-time notification that the message has just been created. I'm doing this. In my controller in the store () method of the controller responsible for the Model Contact, it writes in the database the contact of the frontend form calling the NotificationContact event Staying like this:
public function store(Request $request){
//dados do formulário contato
$result = Contato::create($request->all());
//Evento a ser chamado
\Event::fire(new NotificacaoContato($result));
}
Now I'm not sure how to register this event in AppServiceProvider How would I do that? Continuing. In my .env I set the environment variables as follows:
BROADCAST_DRIVER=pusher
PUSHER_APP_ID="Meu ID"
PUSHER_APP_KEY="Minha chave que eu peguei la no site do push.com "
PUSHER_APP_SECRET="Chave secreta pega no push.com"
Then I uncommented it in my Provider array which is in App \ config \ app.php Just the 2 line in my case
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Soon after In my event itself I did it this way:
class NotificacaoContato implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $contato;
public function __construct($contato)
{
$this->contato = $contato;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('notificacao-contato');
}
}
Now in my View I put the push import to my dashboard backend template along with the script that the push already makes available. Staying like this:
View (dashboard backend Template)
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;
var pusher = new Pusher('aqui vai a minha chave o mesmo do .env', {
cluster: 'us2',
encrypted: true
});
var channel = pusher.subscribe('notificacao-contato');
channel.bind('App\Event\NotificacaoContato', function(data) {
alert('Uma Conatato foi enviado');
});
</script>
What happens to this code. When I submit I submit the form to the store () method on the push.com website it shows that something happens. That my event is called. More then, was not there an alert on my dashboard backend?
Consoleoutput