Laravel Reverb with Presence Channel Events #345
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
This fork of Laravel Reverb introduces internal event dispatching for presence channel activity (
subscribe
andunsubscribe
) at the server level. The primary motivation for this change is to overcome a limitation when using Pusher, while Pusher provides webhook support for presence channel events (likemember_added
andmember_removed
), those events are not directly observable or actionable from within the Laravel backend itself unless relying on external HTTP webhooks.This limitation makes it difficult to:
What this fork adds
This fork introduces the following improvements:
1. Custom Laravel Events for presence channels:
PresenceChannelSubscribe
: Dispatched whenever a user subscribes to a presence channel for the first time (peruser_id
).PresenceChannelUnsubscribe
: Dispatched whenever a user fully disconnects from a presence channel (i.e., no other connections remain with the sameuser_id
).These events are dispatched within the
InteractsWithPresenceChannels
trait and provide access to:Connection
instance (which includesapp id
,socket id
, etc.)2. Better visibility and control:
With these events, your Laravel backend can now:
Example: Listening to the events
You can listen to the new events like any other Laravel events:
Implementation details
The logic is located inside the
InteractsWithPresenceChannels
trait, where:member_added
, the server checks if this is the first connection for thatuser_id
.member_removed
, the server verifies that no other connections remain for the sameuser_id
.This mimics Pusher's behavior while giving you full control in Laravel.