laravel / reverb

Laravel Reverb provides a real-time WebSocket communication backend for Laravel applications.
https://reverb.laravel.com
MIT License
1.05k stars 76 forks source link

Missing 'data' property in 'pusher_internal:subscription_succeeded' event #123

Closed jabools closed 5 months ago

jabools commented 5 months ago

Reverb Version

1.0.0-beta4

Laravel Version

11.1.1

PHP Version

8.3.4

Description

Hello!

This issue targets missing data property in pusher_internal:subscription_succeeded event, in response to connecting to private or public channel.

Overview

Official Pusher server provides following payload for previously mentioned event:

{
  "event": "pusher_internal:subscription_succeeded",
  "data": "{}",
  "channel": "my-channel"
}

This can be easily verfied with JavaScript example after creating sandbox app in Pusher.

Laravel Reverb does not send data property for such event:

{
  "event": "pusher_internal:subscription_succeeded",
  "channel": "my-channel"
}

For JavaScript Pusher-compatible clients it's irrelevant as these can gracefully handle missing property. However, using Laravel Reverb as Pusher server for mobile application on iOS platform - written in Flutter and using forked official Pusher package (https://pub.dev/packages/pusher_channels_flutter) to allow server details customization - makes websocket unusable for apps that rely on that event, as Pusher Swift implementation does not properly parse payload and does not emit subscription_succeeded, which makes it impossible to react for such event.

Solving problem

To make data property available in event payload we could simply remove empty($data) condition in EventHandler::formatPayload method and assign result of JSON encoding directly to that property, as following:

return json_encode(
            array_filter([
                'event' => $prefix.$event,
                'data' => json_encode($data),
                'channel' => $channel,
            ])
        );

But this solution will result in payload containing data: '[]'. To be fully compatible with Pusher official server I suggest to modify mentioned condition and return string '{}' instead of null for empty data, and encoded JSON for other cases.

I've prepared PR for this solution.

Best regards!

Steps To Reproduce

Very basic Laravel application with installed Reverb and JavaScript Echo connecting to websocket channel will provide described results.

driesvints commented 5 months ago

Heya thanks! You seem to have sent a PR so let's see how it goes.