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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With