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

Type error in Reverb with GraphQL subscription (Lighthouse package) #120

Closed wmt-web-pruthvip closed 5 months ago

wmt-web-pruthvip commented 5 months ago

Reverb Version

1.0.0-beta4

Laravel Version

10.48.4

PHP Version

8.2.17

Description

I am using the Laravel Reverb package with GraphQL subscriptions implemented using the Lighthouse package (v6.35.0). When a subscription request is received by Reverb, it throws the following error:

TypeError Laravel\Reverb\Protocols\Pusher\EventHandler::subscribe(): Argument #4 ($data) must be of type ?string, array given, called in /var/www/DemoGraphQL/vendor/laravel/reverb/src/Protocols/Pusher/EventHandler.php on line 29 at vendor/laravel/reverb/src/Protocols/Pusher/EventHandler.php:56

The error is caused by the payload data being an array instead of a string, which is expected by the subscribe method in the EventHandler class.

The payload that triggers the error looks like this:

{
    "event": "pusher:subscribe",
    "data": {
        "channel_data": {
            "user_id": "f76076ed4081e927b1a54bf510992f2c",
            "user_info": []
        },
        "channel": "private-lighthouse-uyeyl2Vnz2MXRHdVMly4lg2YnTKufVqS-1711692828"
    }
}

When running in --debug mode, an additional error is thrown:

TypeError json_decode(): Argument #1 ($json) must be of type string, array given at vendor/laravel/reverb/src/Loggers/CliLogger.php:57

This error occurs because the channel_data value in the payload is also being treated as an array instead of a string.

A possible fix for --debug might be to add a condition check for the channel_data value, similar to the existing check for the data value, before attempting to decode it using json_decode. The condition could be something like && is_string($message['data']['channel_data']).

Additional Information:

If you need any further details about the Lighthouse implementation or any other relevant information, I will provide them to help better understand and resolve the issue.

Steps To Reproduce

start a reverb server running via reverb:start or reverb:start --debug. Use the lighthouse package with the LIGHTHOUSE_BROADCASTER=echo driver.

joedixon commented 5 months ago

@wmt-web-pruthvip to confirm, is this an issue only with debug mode enabled?

wmt-web-pruthvip commented 5 months ago

No, if debug mode is enabled, I get an error at Loggers/CliLogger.php:57. And without it gives at Pusher/EventHandler.php:56.

wmt-web-pruthvip commented 5 months ago

Even though it is working normally (with events), I am able to subscribe to the channel and receive updates. I am only having problems with graphql subscription.

joedixon commented 5 months ago

The payload looks incorrect to me. When subscribing to a private channel, the payload should look more like below. Notice the requirement to pass through an auth token.

{
  "event":"pusher:subscribe",
  "data":{
    "auth":"super-secret-token",
    "channel":"private-channel"
  }
}
wmt-web-pruthvip commented 5 months ago

This payload is set from the lighthouse package here. I think that needs to be updated for support.

Thanks for the update.

joedixon commented 5 months ago

OK, closing this for now.