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

array|null
resolveAuthenticatedUser(Request $request)

Resolve the authenticated user payload for the incoming connection request.

void
resolveAuthenticatedUserUsing(Closure $callback)

Register the user retrieval callback used to authenticate connections.

$this
channel(HasBroadcastChannel|string $channel, callable|string $callback, array $options = [])

Register a channel authenticator.

mixed
verifyUserCanAccessChannel(Request $request, string $channel)

Authenticate the incoming request for a given channel.

array
extractAuthParameters(string $pattern, string $channel, callable|string $callback)

Extract the parameters from the given pattern and channel.

extractParameters(callable|string $callback)

Extracts the parameters out of what the user passed to handle the channel authentication.

extractParametersFromClass(string $callback)

Extracts the parameters out of a class channel's "join" method.

array
extractChannelKeys(string $pattern, string $channel)

Extract the channel keys from the incoming channel name.

mixed
resolveBinding(string $key, string $value, array $callbackParameters)

Resolve the given parameter binding.

mixed
resolveExplicitBindingIfPossible(string $key, mixed $value)

Resolve an explicit parameter binding if applicable.

mixed
resolveImplicitBindingIfPossible(string $key, mixed $value, array $callbackParameters)

Resolve an implicit parameter binding if applicable.

bool
isImplicitlyBindable(string $key, ReflectionParameter $parameter)

Determine if a given key and parameter is implicitly bindable.

array
formatChannels(array $channels)

Format the channel array into an array of strings.

binder()

Get the model binding registrar instance.

callable
normalizeChannelHandlerToCallable(mixed $callback)

Normalize the given callback into a callable.

mixed
retrieveUser(Request $request, string $channel)

Retrieve the authenticated user using the configured guard (if any).

array
retrieveChannelOptions(string $channel)

Retrieve options for a certain channel.

bool
channelNameMatchesPattern(string $channel, string $pattern)

Check if the channel name from the request matches a pattern from registered channels.

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.

mixed
auth(Request $request)

Authenticate the incoming request for a given channel.

mixed
validAuthenticationResponse(Request $request, mixed $result)

Return the valid authentication response.

void
broadcast(array $channels, string $event, array $payload = [])

Broadcast the given event.

HubInterface
getHub()

Get the Mercure hub instance.

void
setHub(HubInterface $hub)

Set the Mercure hub instance.

void
exemptCookieFromEncryption()

Exempt the hub's authorization cookie from cookie encryption.

string
updateData(string[]|null $channels, string $event, array $payload, string|null $socket)

Encode the data of an update targeting the given channels.

Cookie
makeAuthorizationCookie(Request $request, array $grants, mixed $user = null)

Build the subscriber authorization cookie carrying the given grants.

string
broadcastingIdentifier(mixed $user)

Get the broadcasting identifier of the given user.

string
channelTopic(string $channelName)

Build the hub topic of the given channel.

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.

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}).

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.

Parameters

Request $request

Return Value

array|null

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.

Parameters

Closure $callback

Return Value

void

$this channel(HasBroadcastChannel|string $channel, callable|string $callback, array $options = [])

Register a channel authenticator.

Parameters

HasBroadcastChannel|string $channel
callable|string $callback
array $options

Return Value

$this

protected mixed verifyUserCanAccessChannel(Request $request, string $channel)

Authenticate the incoming request for a given channel.

Parameters

Request $request
string $channel

Return Value

mixed

Exceptions

AccessDeniedHttpException

protected array extractAuthParameters(string $pattern, string $channel, callable|string $callback)

Extract the parameters from the given pattern and channel.

Parameters

string $pattern
string $channel
callable|string $callback

Return Value

array

protected ReflectionParameter[] extractParameters(callable|string $callback)

Extracts the parameters out of what the user passed to handle the channel authentication.

Parameters

callable|string $callback

Return Value

ReflectionParameter[]

Exceptions

Exception

protected ReflectionParameter[] extractParametersFromClass(string $callback)

Extracts the parameters out of a class channel's "join" method.

Parameters

string $callback

Return Value

ReflectionParameter[]

Exceptions

Exception

protected array extractChannelKeys(string $pattern, string $channel)

Extract the channel keys from the incoming channel name.

Parameters

string $pattern
string $channel

Return Value

array

protected mixed resolveBinding(string $key, string $value, array $callbackParameters)

Resolve the given parameter binding.

Parameters

string $key
string $value
array $callbackParameters

Return Value

mixed

protected mixed resolveExplicitBindingIfPossible(string $key, mixed $value)

Resolve an explicit parameter binding if applicable.

Parameters

string $key
mixed $value

Return Value

mixed

protected mixed resolveImplicitBindingIfPossible(string $key, mixed $value, array $callbackParameters)

Resolve an implicit parameter binding if applicable.

Parameters

string $key
mixed $value
array $callbackParameters

Return Value

mixed

Exceptions

AccessDeniedHttpException

protected bool isImplicitlyBindable(string $key, ReflectionParameter $parameter)

Determine if a given key and parameter is implicitly bindable.

Parameters

string $key
ReflectionParameter $parameter

Return Value

bool

protected array formatChannels(array $channels)

Format the channel array into an array of strings.

Parameters

array $channels

Return Value

array

protected BindingRegistrar|null binder()

Get the model binding registrar instance.

Return Value

BindingRegistrar|null

protected callable normalizeChannelHandlerToCallable(mixed $callback)

Normalize the given callback into a callable.

Parameters

mixed $callback

Return Value

callable

protected mixed retrieveUser(Request $request, string $channel)

Retrieve the authenticated user using the configured guard (if any).

Parameters

Request $request
string $channel

Return Value

mixed

protected array retrieveChannelOptions(string $channel)

Retrieve options for a certain channel.

Parameters

string $channel

Return Value

array

protected bool channelNameMatchesPattern(string $channel, string $pattern)

Check if the channel name from the request matches a pattern from registered channels.

Parameters

string $channel
string $pattern

Return Value

bool

Collection getChannels()

Get all of the registered channels.

Return Value

Collection

bool isGuardedChannel(string $channel)

Return true if the channel is protected by authentication.

Parameters

string $channel

Return Value

bool

string normalizeChannelName(string $channel)

Remove prefix from channel name.

Parameters

string $channel

Return Value

string

__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.

Parameters

HubInterface $hub
int $expiration
ChannelEncrypter|null $encrypter
string $topicPrefix
bool $clientEvents

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.

Parameters

Request $request

Return Value

mixed

Exceptions

AccessDeniedHttpException
BroadcastException

mixed validAuthenticationResponse(Request $request, mixed $result)

Return the valid authentication response.

Parameters

Request $request
mixed $result

Return Value

mixed

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.

Parameters

array $channels
string $event
array $payload

Return Value

void

Exceptions

BroadcastException

HubInterface getHub()

Get the Mercure hub instance.

Return Value

HubInterface

void setHub(HubInterface $hub)

Set the Mercure hub instance.

Parameters

HubInterface $hub

Return Value

void

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.

Return Value

void

protected string updateData(string[]|null $channels, string $event, array $payload, string|null $socket)

Encode the data of an update targeting the given channels.

Parameters

string[]|null $channels
string $event
array $payload
string|null $socket

Return Value

string

Exceptions

JsonException

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.

Parameters

Request $request
array $grants
mixed $user

Return Value

Cookie

Exceptions

BroadcastException

protected string broadcastingIdentifier(mixed $user)

Get the broadcasting identifier of the given user.

Parameters

mixed $user

Return Value

string

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.

Parameters

string $channelName

Return Value

string

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.

Parameters

string $channelName

Return Value

string

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.

Parameters

string $channelName

Return Value

string

See also

https://mercure.rocks/docs/hub/concepts/active-subscriptions