Echo: how to emit a Laravel event through the WebSocket?
For example, if I'm making a simple chat app I can only receive messages from a WebSocket, but I can't send them to the WebSocket and handle in Laravel? I can use whispering, but in that case, Laravel won't receive it anyway. So the only way to store chat messages on the server is to use HTTP requests for outgoing messages? Am I a right?
In that case, it looks like the power of full duplex WebSocket protocol isn't used.
In your chat app, when a user leaves a message you should fire an event on your client side which triggers an AJAX call to your server (Laravel). At your, server side you should persist this message and dispatch an event to websockets. And at your client side you listen for that event from websocket. Thats it!
For broadcasting to websockets take a look at docs
fire an event on your client side which triggers an AJAX call to your server (Laravel)
Exactly. That's what I'm talking about. The only way to send something from a client (JS) to the server (Laravel) is to make AJAX-requests. Currently, it's impossible with WebSockets.
Websockets form a connection between client and server, why would you emit event to the server while you can make ajax call. You cannot rely on client for that. If it was possible through websockets, I think It would involve some security issues.
I am not an expert with websockets though.
Why not? If a WebSocket connection is already established, auth is already passed sending messages in both directions is a logical step. Furthermore, it's much more efficient.
So currently, WebSocket uses 50% of its power. Laravel might use Server-Side Events with the same result.
Laravel can listen for Redis pub/sub. Laravel Echo should allow a client emit an event, send it through a WebSocket connection and publish on a Redis. Laravel should listen and react. Strange that it's not implemented yet.