I am using laravel reverb. This is my laravel controller function for storing the form data and sending the notification.
public function store(Request $request)
{
$validatedData = $request->validate([
'last_name' => 'required|string|max:255',
'room_number' => 'required|string|max:255',
'request_text' => 'required|string',
'request_status' => 'required|string',
'request_type' => 'required|string|max:255',
]);
$requestItem = RequestItem::create($validatedData);
$emailData = [
'name' => $requestItem->last_name,
'room' => $requestItem->room_number,
'subject' => ucfirst($requestItem->request_type),
'requestType' => $requestItem->request_type,
'additionalInfo' => $requestItem->request_text,
'requestStatus' => $requestItem->request_status,
];
$type = $request->input('request_type');
$message = "New {$type} submitted";
$notification = Notification::create([
'type' => $type,
'message' => $message,
'read' => false,
]);
Log::info('BroadcastNotificationEvent dispatched successfully');
event(new BroadcastNotificationEvent($notification));
$recipients = [''];
Mail::to($recipients)->send(new HotelServicesMail($emailData));
return response()->json($requestItem, 201);
}
This is the react function where i am trying to access the notification sent from the backend.
useEffect(() => {
if (window.Echo) {
console.log('Echo is available');
setStatus('Connecting...');
window.Echo.channel('notifications')
.listen('BroadcastNotificationEvent', (e) => {
console.log('Received notification:', e);
setNotifications(prevNotifications => [e.notification, ...prevNotifications]);
setUnreadCount(prevCount => prevCount + 1);
});
window.Echo.connector.pusher.connection.bind('connected', () => {
setStatus('Connected');
console.log('WebSocket connected');
});
window.Echo.connector.pusher.connection.bind('error', (err) => {
setStatus(`Error: ${err.message}`);
console.error('WebSocket error:', err);
});
return () => {
window.Echo.leaveChannel('notifications');
};
} else {
console.error('Echo is not available');
setStatus('Echo not initialized');
}
}, []);
But i dont receive any notification when i send the submit the form.
The socket connection is being established but i am not receiving any exchange of data happening.
<?php
namespace AppEvents;
use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcast;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
class BroadcastNotificationEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*/
// public $type;
// public $message;
public $notification;
/**
* Create a new event instance.
*
* @param string $type
* @param string $message
*/
// public function __construct($type, $message)
// {
// //
// $this->type = $type;
// $this->message = $message;
// }
public function __construct($notification)
{
$this->notification = $notification;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, IlluminateBroadcastingChannel>
*/
public function broadcastOn():array
{
return [ new Channel('notifications')];
}
/**
* The event's broadcast name.
*
* @return string
*/
This is what the event in laravel looks like.
New contributor
deepak k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.