ably-forks / laravel-echo

Laravel Echo library for beautiful Pusher and Ably integration.
https://laravel.com/docs/broadcasting#client-side-installation
MIT License
12 stars 4 forks source link

401 Error on Subsequent Private/Public Channel Subscriptions #36

Closed zorenkonte closed 3 months ago

zorenkonte commented 3 months ago

Echo Version

1.0.4

Laravel Version

8.83.27

PHP Version

8.3.6

NPM Version

10.5.2

Database Driver & Version

No response

Description

I'm encountering a 401 (unauthorized) error when attempting to subscribe to private channels using Laravel Echo and Ably integration. This issue arose after switching from Pusher to Ably because im having an issue about presence channel (where someone also experienced similar problems - https://stackoverflow.com/questions/57297057/laravel-echo-server-detect-user-disconnection). The first call to a public channel works successfully, but subsequent calls to private channels fail. Reordering the calls doesn't resolve the issue. I dont have problems with authentication before I switch to ably as broadcaster.

Expected Behavior:

All channel subscriptions (public and private) should be successful using the provided authentication mechanism.

Actual Behavior:

  1. window.Echo.join(channels.online) (public channel) subscribes successfully.
  2. Subsequent calls to window.Echo.private(...) (private channels) result in a 401 error.

Here is my full setup

  window.Ably = Ably

  window.Echo = new Echo({
    broadcaster: 'ably'
  })

  window.Echo.join('online') //works
  window.Echo.join('chat-1') // unauthenticated
  window.Echo.private('account.1') // unauthenticated
  window.Echo.private('user.1') //unauthenticated

  // reorder

  window.Echo.private('account.1') //works
  window.Echo.join('online')  // unauthenticated
  window.Echo.join('chat-1') // unauthenticated
  window.Echo.private('user.1') //unauthenticated

Steps To Reproduce

Steps to Reproduce:

  1. Clone and install this repo https://github.com/cretueusebiu/laravel-vue-spa.
  2. Set up Laravel Echo with Ably integration following the official documentation.
  3. In your application code, subscribe to a public channel first:
window.Echo.join('online')
  1. Then attempt to subscribe to a private channel:
window.Echo.private('account.1')
  1. Repeat step 4 for additional private channels (e.g., window.Echo.private('user.2')).

Expected Result:

All channel subscriptions should succeed.

Actual Result:

Only the first channel subscription succeeds. Subsequent channel subscriptions result in a 401 error.

Additional Information:

I would appreciate any guidance on how to resolve this issue and successfully subscribe to private channels using this package.

sacOO7 commented 3 months ago

Hi @zorenkonte I am not sure if you have set up auth. for ably properly. I would recommend you to go through following docs carefully

  1. https://github.com/ably/laravel-broadcaster/blob/main/README.md ( also includes migrating section )
  2. https://github.com/ably-forks/laravel-echo#readme
  3. You can setup https://github.com/ably-labs/laravel-broadcast-app using docker, and take a look at server side https://github.com/ably-labs/laravel-broadcast-app/blob/main/routes/channels.php and https://github.com/ably-labs/laravel-broadcast-app/blob/main/routes/api.php including provided comments

requestTokenFn is mostly not required when auth is implemented internally. You just need to specify authEndpoint as a part of clientOptions. You should check https://github.com/ably-forks/laravel-echo?tab=readme-ov-file#installation. Also make sure you are using ably version <=1.x as specified in the install command. If issue still persists, you can create code with dummy repo. where issue can be reproduced and we can check where things are going wrong

zorenkonte commented 3 months ago

Hi @sacOO7,

Thank you for your reply. I apologize for the delayed response. I stopped working on this issue after a few attempts and moved on to another task. However, I recently revisited it and believe I have identified the cause of the 401 error.

I am using jwt-auth for authentication, which by default checks the token payload in the request. Ably Laravel Broadcaster also returns and find a token with each authentication request. As a result, jwt-auth interprets this as the token authentication code, which causes a 401 error on subsequent requests because Ably Laravel Broadcaster's token payload conflicts with jwt-auth

Commenting out the InputSource resolved the issue for me or by following the instruction from this reply.

However, this solution could be improved. It would be beneficial to provide an option for a custom token key name in the payload to avoid conflicts with other authentication methods.

sacOO7 commented 3 months ago

Hi @zorenkonte thanks for the response! I have created a separate issue for the same -> https://github.com/ably-forks/laravel-echo/issues/37 👍

zorenkonte commented 3 months ago

Thank you, @sacOO7 Closing this issue now.