MercureBroadcaster
class MercureBroadcaster extends Broadcaster (View source)
Traits
Properties
| protected Closure|null | $authenticatedUserCallback | The callback to resolve the authenticated user information. |
from Broadcaster |
| protected array | $channels | The registered channel authenticators. |
from Broadcaster |
| protected array | $channelOptions | The registered channel options. |
from Broadcaster |
| protected BindingRegistrar|null | $bindingRegistrar | The binding registrar instance. |
from Broadcaster |
Methods
Resolve the authenticated user payload for the incoming connection request.
Register the user retrieval callback used to authenticate connections.
Register a channel authenticator.
Authenticate the incoming request for a given channel.
Extract the parameters from the given pattern and channel.
Extracts the parameters out of what the user passed to handle the channel authentication.
Extracts the parameters out of a class channel's "join" method.
Extract the channel keys from the incoming channel name.
Resolve the given parameter binding.
Resolve an explicit parameter binding if applicable.
Resolve an implicit parameter binding if applicable.
Determine if a given key and parameter is implicitly bindable.
Format the channel array into an array of strings.
Normalize the given callback into a callable.
Retrieve the authenticated user using the configured guard (if any).
Retrieve options for a certain channel.
Check if the channel name from the request matches a pattern from registered channels.
Return true if the channel is protected by authentication.
Remove prefix from channel name.
Create a new broadcaster instance.
Return the valid authentication response.
Broadcast the given event.
Get the Mercure hub instance.
Set the Mercure hub instance.
Exempt the hub's authorization cookie from cookie encryption.
Encode the data of an update targeting the given channels.
Build the subscriber authorization cookie carrying the given grants.
Get the broadcasting identifier of the given user.
Build the hub topic of the given channel.
Build the whisper topic of the given channel: the only topic its subscribers may publish to, keeping the channel topic server-only.
Build the subscription-API topic matcher for the given presence channel, matching both the snapshot listing used to seed the member list (.../subscriptions/{match_type}/{match}) and the per-subscriber active:true/false events (.../{match_type}/{match}/{subscriber}).
Details
array|null
resolveAuthenticatedUser(Request $request)
Resolve the authenticated user payload for the incoming connection request.
See: https://pusher.com/docs/channels/library_auth_reference/auth-signatures/#user-authentication.
void
resolveAuthenticatedUserUsing(Closure $callback)
Register the user retrieval callback used to authenticate connections.
See: https://pusher.com/docs/channels/library_auth_reference/auth-signatures/#user-authentication.
$this
channel(HasBroadcastChannel|string $channel, callable|string $callback, array $options = [])
Register a channel authenticator.
protected mixed
verifyUserCanAccessChannel(Request $request, string $channel)
Authenticate the incoming request for a given channel.
protected array
extractAuthParameters(string $pattern, string $channel, callable|string $callback)
Extract the parameters from the given pattern and channel.
protected ReflectionParameter[]
extractParameters(callable|string $callback)
Extracts the parameters out of what the user passed to handle the channel authentication.
protected ReflectionParameter[]
extractParametersFromClass(string $callback)
Extracts the parameters out of a class channel's "join" method.
protected array
extractChannelKeys(string $pattern, string $channel)
Extract the channel keys from the incoming channel name.
protected mixed
resolveBinding(string $key, string $value, array $callbackParameters)
Resolve the given parameter binding.
protected mixed
resolveExplicitBindingIfPossible(string $key, mixed $value)
Resolve an explicit parameter binding if applicable.
protected mixed
resolveImplicitBindingIfPossible(string $key, mixed $value, array $callbackParameters)
Resolve an implicit parameter binding if applicable.
protected bool
isImplicitlyBindable(string $key, ReflectionParameter $parameter)
Determine if a given key and parameter is implicitly bindable.
protected array
formatChannels(array $channels)
Format the channel array into an array of strings.
protected BindingRegistrar|null
binder()
Get the model binding registrar instance.
protected callable
normalizeChannelHandlerToCallable(mixed $callback)
Normalize the given callback into a callable.
protected mixed
retrieveUser(Request $request, string $channel)
Retrieve the authenticated user using the configured guard (if any).
protected array
retrieveChannelOptions(string $channel)
Retrieve options for a certain channel.
protected bool
channelNameMatchesPattern(string $channel, string $pattern)
Check if the channel name from the request matches a pattern from registered channels.
Collection
getChannels()
Get all of the registered channels.
bool
isGuardedChannel(string $channel)
Return true if the channel is protected by authentication.
string
normalizeChannelName(string $channel)
Remove prefix from channel name.
__construct(HubInterface $hub, int $expiration = 300, ChannelEncrypter|null $encrypter = null, string $topicPrefix = 'https://laravel.alt/echo/', bool $clientEvents = true)
Create a new broadcaster instance.
The hub's token factory mints the subscriber cookie token and must be non-null, already carrying the static RFC 9068 claims; the hub's token provider mints the (longer-lived) publish token. The encrypter enables end-to-end encrypted channels: without one, using such a channel throws. Presence channels are never encrypted, as their member payloads flow through the hub's subscription API.
The topic prefix namespaces every hub topic: it keeps Laravel topics from colliding with other publishers sharing the hub, and lets two applications sharing one hub (and one JWT secret) stay apart. The default is a "laravel.alt" URL: ".alt" is reserved outside the DNS (RFC 9476), so the IRI is guaranteed non-resolvable and unsquattable.
mixed
auth(Request $request)
Authenticate the incoming request for a given channel.
Mercure multiplexes every joined topic over one EventSource guarded by one authorization cookie, so this reads a "channel_names" array and mints a single token covering every currently-joined channel, public ones included (a hub without the "anonymous" directive rejects token-less subscribers). Authorization is per channel: a denied channel is flagged in the response and left out of the grants, so revoking one channel mid-session never takes down the others.
Each authorized end-to-end encrypted channel's response entry carries the JSON Web Key decrypting its updates, the out-of-band key exchange recommended by the Mercure specification: the hub never sees the keys.
Every guarded channel also gets a whisper topic the subscriber may publish to, so clients can exchange whispers directly through the hub. Confining client publish rights to those topics keeps the channel topics server-only: a whisper grant can never forge a server event.
mixed
validAuthenticationResponse(Request $request, mixed $result)
Return the valid authentication response.
void
broadcast(array $channels, string $event, array $payload = [])
Broadcast the given event.
The hub delivers a private update to any subscriber authorized for one of its topics, so every guarded channel gets its own update: a shared one would leak the sibling channel names to every recipient. Encrypted channels additionally hide the event name, payload, and socket ID from the hub inside a JWE; the routing envelope stays plaintext so a multiplexing subscriber can select the decryption key.
HubInterface
getHub()
Get the Mercure hub instance.
void
setHub(HubInterface $hub)
Set the Mercure hub instance.
protected void
exemptCookieFromEncryption()
Exempt the hub's authorization cookie from cookie encryption.
Encrypting the raw JWT would produce a value the hub can never verify.
protected string
updateData(string[]|null $channels, string $event, array $payload, string|null $socket)
Encode the data of an update targeting the given channels.
protected Cookie
makeAuthorizationCookie(Request $request, array $grants, mixed $user = null)
Build the subscriber authorization cookie carrying the given grants.
The Authorization helper mints the token with the hub's factory, resolves the cookie domain so a hub on a sibling subdomain still receives the cookie, and injects an "exp" claim matching the cookie lifetime. Only the per-request "sub" claim is contributed here; the static RFC 9068 claims come from the hub's factory.
protected string
broadcastingIdentifier(mixed $user)
Get the broadcasting identifier of the given user.
protected string
channelTopic(string $channelName)
Build the hub topic of the given channel.
Channel names are RFC 3986 encoded into a single path segment, so a name can never escape its namespace, whatever characters it contains.
protected string
whisperTopic(string $channelName)
Build the whisper topic of the given channel: the only topic its subscribers may publish to, keeping the channel topic server-only.
protected string
subscriptionPattern(string $channelName)
Build the subscription-API topic matcher for the given presence channel, matching both the snapshot listing used to seed the member list (.../subscriptions/{match_type}/{match}) and the per-subscriber active:true/false events (.../{match_type}/{match}/{subscriber}).
"{/:subscriber}?" is an optional URLPattern group covering the trailing segment and its slash.