Replies: 1 comment
-
I would like to explain my perspective on this topic, but before I start I want to answer a few questions that were asked here.
At the server side, Laravel doesn't have any idea about Pusher subscribers and whether they're connected or disconnected.
There's no need. A single call to the It's as simple as calling it like broadcast(new ChatMessage($message)); Regarding the client-side part, the problem with Laravel Echo is the way it's tied to the Pub/Sub architecture, so you can't actually encapsulate the |
Beta Was this translation helpful? Give feedback.
-
Let's discuss how we can empower Laravel apps with AnyCable.
The discussion started here: beyondcode/laravel-websockets#190 (comment)
The idea is to start with a basic integration similar to our Node.js one: https://docs.anycable.io/guides/serverless
There are two main options to consider for the integration:
Integration with Laravel Echo
Laravel Echo already provides APIs to implement real-time features in Laravel apps. Currently, Echo only support pub/sub channels and broadcasting, but Reverb is adding client-to-server commands support making Echo Channels bi-directional.
Making AnyCable compatible with Echo would be a huge win, because it would allow Laravel developers to use the framework they know without learning anything new.
There are three integration points:
Server-to-client broadcasts
We need to implement a custom Broadcaster.
In the
broadcast
function, we MUST perform an HTTP request to broadcast a message to the channel: https://docs.anycable.io/ruby/broadcast_adapters?id=http-adapterThe
auth
method can be similar to other adapters (it's not related to AnyCable); in thevalidAuthenticationResponse
we MAY generate a JWT identification token for AnyCable (to authenticate connection at the WS server side without making RPC calls) and a signed stream name for the channel (similar to Turbo Streams.Client-to-server events/messages handling (RPC)
AnyCable communicates with the application over RPC (HTTP or gRPC) to handle WebSocket connection lifecycle events (Connect/Disconnect) and incoming messages (Commands).
Let's start with the HTTP version and implement a middleware to process AnyCable RPC requests.
The API schema (with examples) can be found here: anycable.stoplight.io.
Since Echo relies on HTTP request to authorize the access, we don't necessary need to implement Connect and Command with the
"command": "subscribe"
requests (we can assume that JWT and signed streams used instead).We mostly care about handling incoming commands and processing disconnect events.
To handle incoming commands, we may use the API proposed here:
Thus, we need somehow to turn the following RPC payload into the command execution above:
Questions:
How does Laravel Echo handle client disconnection (at the server side)?
What would be an API to send a message from the server to the client in response to the command (smth like
this->$transmit(...)
)?Wispers
Laravel Echo also supports wispering, or sending messages to the channel without interacting with the app.
To support this, we would likely need to implement a custom routing logic at the Go server side (similar to Turbo Streams, etc.). Or we can promote this feature to be generic (not Laravel-specific).
Client-side (JS) integration
There are two options:
We can implement a custom connector on top of anycable-client
We can add Pusher protocol support to AnyCable, so we can re-use existing Pusher connector.
I would start with the first one for the PoC, and as soon as we cover all the other parts, I would go with the Pusher protocol implementation.
Standalone AnyCable for Laravel integration
We can forget about Echo/Reverb and bring Action Cable-like channels to Laravel (similarly how we did for JS in our JS SDK).
You can learn more about conceptual differences between Echo and Action Cable protocols/abstractions here: https://anycable.substack.com/p/any-cables-monthly-18.
This approach doesn't require any client-side changes (we can use anycable-client) and hacking around Laravel Broadcasting.
I think, we can still re-use event classes, but not channels (or maybe we can extend channels to support
subscribe
andunsubscribe
callbacks).The API (RPC) part must be implemented in full.
I believe this approach can be more beneficial (since I personally like Action Cable abstraction more), but it's gonna be a hard sell for the community (competing with the official solution is not a good idea).
Still, I think, it's worth considering.
Known limitations
Currently, AnyCable has no built-int Presence tracking mechanism. Though it's expected to come later this year, and having Laravel as a potential user of this feature would be great for fine-tuning.
Beta Was this translation helpful? Give feedback.
All reactions