Create reactphp socket for push notification in laravel - laravel

I want to use reactphp socket for push notifications in laravel but I do not know what to do?
route:
Route::get('/test', function () {
event(new testEvent($test));
});
event:
public $test;
public function __construct($test)
{
$this->test = $test;
}
public function broadcastOn()
{
return new PrivateChannel('test.' . $this->test->id);
}
channel:
Broadcast::channel('test.{test}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Note: I do not want to use the pusher

Related

Why I can not get message pusher in Laravel?

Here is a private channel:
public function broadcastOn()
{
// Here I get $user object successfully
return new PrivateChannel('user-'.$this->user->id); // it is 1
}
The permissions are:
Broadcast::channel('user-{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
The sender is:
Route::post('sender', function () {
PrivateEvent::dispatch(Auth::user());
})->middleware('auth:sanctum');
The client was tested in pusher debug console, it works:
var channel = pusher.subscribe('user-1');
channel.bind('PrivateEvent', function(data) {
alert(JSON.stringify(data));
});
Why when I send message using routing I dont get the message in JS? JS has not errors too.

Laravel Broadcast send message to multiple user. Can we send message to particular user

Channel.PHP
These is my Channels.php File.
Broadcast::channel('privatechat.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
ChatComponent.vue
There is my ChatCompnent.vue file
Echo.private('privatechat.'+props.user.id)
.listen('SendMessage', (e) => {
messages.value.push({
message: e.message.message,
user: e.user
// reciver_id: props.userid
});
})
Events/SendMessage.php
There is my Events File
public function broadcastOn()
{
// dd($this->message->rec_id);
// $newiadid = $this->message->rec_id;
// dd($newiadid);
return new PrivateChannel('privatechat.'.$this->message->rec_id);
}
ChatController.php
There is my Controller File
public function messagesStore(Request $request) {
$user = Auth::user();
$reccid = $request->rec_id;
$messages = $user->messages()->create([
'message' => $request->message,
'rec_id' => $request->rec_id
]);
broadcast(new SendMessage($user, $messages))->toOthers();
return 'message sent';
}

laravel broadcasting Broadcasting in private chanel not working i use laravel echo

event is:
public $chat ;
public function __construct($chat)
{
$this->chat = $chat;
}
public function broadcastOn()
{
// return new Channel('recieve-chat');
return new PrivateChannel('recieve-chat' );
}
routes/channels.php is:
Broadcast::channel('recieve-chat', function ($user ) {
return true;
// return $user->id === $reciever_id;
});
in blade file:
<script>
window.addEventListener('DOMContentLoaded' , function () {
Echo.private('recieve-chat')
.listen('ChatBroad', (e) => {
window.livewire.emit('recieve:' + e.chat.bid_id , e.chat);
$(chatScrollDown('.chat'+ e.chat.bid_id ));
});
});
</script>
broadcast Channel work properly. but in PrivateChannel is not working. and in console not showing any error
i use laravel echo and pusher
in App\Providers\BroadcastServiceProvider.php
public function boot()
{
Broadcast::routes();
require base_path('routes/channels.php');
}
must like the code. in Broadcast::routes() we dont need any middleware.

Broadcast channels not being registered in Laravel

I've been stuck with Broadcasting on private channel in authentication part. What I have done is, I made custom authEndpoint in Echo server
"authEndpoint": "/broadcastAuth",
Controller:
public function broadcastAuth(Request $request){
$channel = $this->normalizeChannelName($request->channel_name);
$broadcaster = new CustomBroadcaster();
$channelAuth = $broadcaster->verifyUserCanAccessChannel($request, $channel);
if($channelAuth){
return true;
}
return response()->json([
'message' => 'Not allowed'
], 403);
}
I have made a custom broadcaster class that extends Illuminate\Broadcasting\Broadcasters\Broadcaster
so that I can override verifyUserCanAccessChannel method
My verifyUserCanAccessChannel method:
public function verifyUserCanAccessChannel($request, $channel)
{
Log::info($this->channels);
foreach ($this->channels as $pattern => $callback) {
if (! $this->channelNameMatchesPattern($channel, $pattern)) {
continue;
}
$parameters = $this->extractAuthParameters($pattern, $channel, $callback);
$handler = $this->normalizeChannelHandlerToCallable($callback);
if ($result = $handler($request->user(), ...$parameters)) {
return $this->validAuthenticationResponse($request, $result);
}
}
throw new AccessDeniedHttpException;
}
Here I have logged $this->channels to get the registered channels from routes/channel.php
But the result is empty.
Here is my BroadcastServiceProvider:
public function boot()
{
require base_path('routes/channels.php');
}
Here is my channel.php:
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('post-comment.{id}', function ($user, $id){
\Illuminate\Support\Facades\Log::info($user);
return true;
});
And I have uncommented both in config/app.php
Illuminate\Broadcasting\BroadcastServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,
What could be the mistake here. Please help me figure this out.
Thank you in advance.

Why I don't receive broadcast message Laravel Pusher?

My event broadcast looks as:
class OrderStatusUpdate implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $username;
public $message;
public function broadcastAs()
{
return 'my-event';
}
public function __construct($username)
{
$this->username = $username;
$this->message = "Order status by {$username} was changed";
}
public function broadcastOn()
{
return ['my-event'];
}
}
Call event like:
Route::get('/', function () {
OrderStatusUpdate::dispatch('Nick');
return "Event has been sent!";
});
Client side is:
var pusher = new Pusher('018814b79958ee4f2ad1d', {
cluster: 'eu',
forceTLS: true
});
var channel = pusher.subscribe('my-event');
channel.bind('my-event', function(data) {
alert(JSON.stringify(data));
});
All .env settings are correct!
As you can see I use default Laravel settings and channel name. But message comes with error in Pusher server. But I dont get them on HTML page, with code JS showed upper.
Let me explain from client side:
var channel = pusher.subscribe('order');
channel.bind('OrderStatusUpdate', function(data) {
alert(JSON.stringify(data));
});
order id name of channel
OrderStatusUpdate is event name Event
It mean server should be:
public function broadcastAs()
{
return 'OrderStatusUpdate'; // Event name
}
public function broadcastOn()
{
return ['order'];
}
But I am not sure about this array sintax: return ['order'];, try instead this:
public function broadcastOn()
{
return new Channel("order");
}

Resources