Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data to laravel echo websocket from client to server

I am developing a Laravel app based on Websockets using Laravel Echo.

Everything works fine on the server side, when I send data (through the visual interface of Laravel-Websockets from the browser) they appear in real time in the browser console.

Such that: (client side)

Echo.channel('testchannel')
    .listen('TestEvent', (e) => {
        console.log(e.message);
    });

But then I also want to be able to send data from the client side to the server (as it would be done with the Websocket:send() method) and for this I use the following line of code:

Echo.connector.socket.emit('App/Events/TestEvent', 'testchannel', {"message":"Hello World"});

However, the following error appears in the browser console:

TypeError: Echo.connector.socket is undefined

like image 636
Héctor Manuel Martínez Durán Avatar asked Nov 07 '22 12:11

Héctor Manuel Martínez Durán


1 Answers

Based on my experience, I think it is better to post your data to your backend and broadcast it in your controller.

broadcast(new App/Events/TestEvent())->toOthers();

However, this is my idea. if there is a better solution i would like to benefit.

like image 107
Mohammad Aliyari Avatar answered Nov 15 '22 05:11

Mohammad Aliyari