John Davidson

php - How can I find the information sent from the laravel websocket web server?

0 comments
Message:


I use the websocket server to send the information of an event as a broadcast.
But I can not get information through laravel echo .
this is my config for laravel and websocket server.


broadcasting config


    'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
// 'encrypted' => true,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http',
//disable cros orgin
'useTLS' => false,
],
],

websockets config


 'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'path' => env('PUSHER_APP_PATH'),
'capacity' => null,
'enable_client_messages' => false,
'enable_statistics' => true,
],
],

env config


PUSHER_APP_ID=your-pusher-app-id
PUSHER_APP_KEY=your-pusher-key
PUSHER_APP_SECRET=your-pusher-secret
PUSHER_APP_CLUSTER=mt1
LARAVEL_WEBSOCKETS_PORT=6001


boostrap.js



window.Echo = new Echo({
broadcaster: 'pusher',
key: "your-pusher-key",
cluster: "mt1",
wsHost: window.location.hostname,
wsPort: 6001,

//two config above disable crose orgin
disableStats: true,
forceTLS: false,

});



My event file



class OrderStatusChange implements ShouldBroadcast{ 

use Dispatchable, InteractsWithSockets, SerializesModels;

public $message;
public $user;

public function __construct($message,$user)
{
$this->message = $message;
$this->user = $user;
}


public function broadcastOn()
{
return new Channel('notifications');
}

}



and laravel-echo



 window.Echo.channel('notifications').listen('OrderStatusChange', (e) => {
console.log(e.data);
console.log(e);
console.log('window');

});



and console da



Pusher :  : ["State changed","initialized -> connecting"] app.js:38851:32

Pusher : : ["Connecting",{"transport":"ws","url":"ws://shop.local:6001/app/your-pusher-key?protocol=7&client=js&version=7.0.2&flash=false"}] app.js:38851:32
Pusher : : ["State changed","connecting -> connected with new socket ID 307045135.646205772"] app.js:38851:32
Pusher : : ["Event sent",{"event":"pusher:subscribe","data":{"auth":"","channel":"order"}}]
Pusher : : ["Event recd",{"event":"pusher_internal:subscription_succeeded","channel":"order"}] app.js:38851:32
Pusher : : ["No callbacks on order for pusher:subscription_succeeded"]



My Answer:
It looks like you are trying to listen for the `OrderStatusChange` event on the `notifications` channel using Laravel Echo.

In your Laravel Echo setup, you are correctly specifying the channel and event to listen to. However, in your event file, you are not broadcasting the event on the correct channel.

In your `broadcastOn()` method in the `OrderStatusChange` event file, you are returning a new Channel('notifications'). This should match the channel you are listening to in your Laravel Echo setup.

Make sure that the channel you are broadcasting on matches the channel you are listening to in your Laravel Echo setup.

Additionally, in your Laravel Echo setup, you are listening for the `OrderStatusChange` event, but in your event file, you are not broadcasting the event with that name. Make sure that the event name you are broadcasting matches the event name you are listening to in Laravel Echo.

Once you have made these changes, you should be able to receive the information sent from the Laravel WebSocket server in your Laravel Echo setup.

Rate this post

5 of 5 based on 8987 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us