1
What do I need? I need when a user in the frontend sends a message to the system and the system (Dashboard backend) to receive a real-time notification that is the message just created. I’m doing it like this. In my controller in the store() method of the controller responsible for the Model Contact, it records in the database the contact form of the frontend calling the event Notificacaocontato Thus remaining:
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 don’t know how to register this event in Appserviceprovider How would I do that? Moving on. In my . env 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 decoded it in my Provider array which is in App config app.php Just that the 2 line in my case
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Right after In my Event itself I did so:
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 have imported the one from push to my Dashboard backend template along with the script that push already provides. That being so:
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 with this code. When I send it, I submit the form to the store() method on the push.com website and it shows that something happens. That my event is called. More pera ai, it was not to appear the Alert in my backend Dashboard?
Um. I get it. But let me ask you a few more questions Lorenzo. No num. 3) what is this action you’re talking about where this file is? $options = [ 'cluster' => 'us2', 'Encrypted' => true ]; $Pusher = new Pusher( 'xxx', 'yyy', 'zzz', $options ); That’s in my controller ? That part I missed. And what would that yyy zzz xxx ?
– Natan Melo
Lorenzo, I understand that @Natanmelo calls "Dashboard backend" is an administrative area of the application itself. So it wouldn’t be the other way around like you said. The need remains for the server to send a message to the front end (administrative). Natan, please confirm that’s right.
– bfavaretto
Rightly. When an internet user sends in the contact form to the administrative area I receive there in the administrative area a notification. From user front end -> to aministratriva area (Backend Dashboard) . @bfavaretto
– Natan Melo
I edited the post and I’m showing the output that is giving. More in my view still not appearing anything. Now I did so When I publish an article in my main view appears an alert or whatever saying that was posted a new article @bfavaretto
– Natan Melo
Caramba I solved this po** Putz that complicated thing for me to put to work. More I am very happy.
– Natan Melo
@bfavaretto I understood then. But I was referring to the theory even when I meant that it was the opposite. It was just to clarify that the idea of using a service that uses web sockets is to allow the server to have "autonomy" to communicate with the client without a request from the client (as is the standard of the web architectures). :)
– Lorenzo Fernandez
Arthur, I’m glad you made it. But to answer your previous question. The action I was referring to was your method that would receive the request, your Controller. And the "xxx", "yyy" and "zzz" were just meant to represent that there should be your credentials hug!
– Lorenzo Fernandez