Why isn't the "Broadcast Only To Others" functionality working in Laravel with the Ably broadcaster?

 

Caution:
The information on this page discusses using Laravel broadcaster with the Ably Pusher adapter to resolve an issue with broadcast only to others not working when using pusher-js on the client side and the Ably Laravel broadcaster on the server side.

However it is highly recommend to use the ably-js based ably-laravel-echo client framework on the client side and the Ably Laravel broadcaster on the server side which fixes this issue.

The ably-laravel-echo is the successor to the pusher-client based ably broadcaster.

 
The issue comes because the Laravel docs currently recommend using a Pusher client on the javascript side (using the Ably Pusher protocol adapter), but the Ably laravel broadcaster on the server side. The 'only broadcast to others' is part of the interaction between serverside and clientside parts of the Pusher protocol adapter, it doesn't work if you mix and match (with the current Laravel Ably broadcaster implementation, you just end up with an extra "socket" field in your data payload).

The simplest fix is switch to using the Pusher Laravel broadcaster, but pointed at the Ably Pusher adapter endpoint, similar to how the setup is done clientside. So in broadcasting.php, something like:
'pusher' => [
'driver' => 'pusher',
'key' => '<appid.keyid>',
'secret' => '<secret>',
'app_id' => '<appid>',
'options' => [
'scheme' => 'https',
'host' => 'rest-pusher.ably.io',
'port' => 443,
],
],
An alternative would be to go 'all Ably', using an Ably client library on the client side and the Ably broadcaster for Laravel. Unfortunately this is not very well support by Laravel Echo yet, so if going this route you would set up ably-js yourself, and pass the connectionKey up to the serverside broadcaster to use the "broadcast only to others" functionality, see https://ably.com/documentation/rest/messages#publish-on-behalf .
Or, of course, just have the client publish the message to Ably itself (and set the echoMessage client option to false to prevent it being echoed back to the client). With Ably, unless there is a good reason you need to have every publish go through your servers, we recommend that the client just publish the message itself, which will result in much lower latency publishes.