happones / nativescript-laravel-echo

A nativescript plugin for laravel-echo
Apache License 2.0
10 stars 2 forks source link

TnsEcho options have more than one format to send the authorization bearer #13

Open MateusSpadari opened 4 years ago

MateusSpadari commented 4 years ago

Hello @happones !

In your documentation there is an example format for sending authentication bearer, but in the Laravel documentation shows a different format! I would like to know the correct format to use, since the backend is not receiving this authentication.

Captura de Tela 2019-11-21 às 11 38 40

both formats in the picture. Thanks!

happones commented 4 years ago

Correct way

auth: {
   headers: {
      Authorization: ....
   }
}

Make sure to modify BroadcastServiceProvider

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes(['middleware' => 'your_middleware']);

        require base_path('routes/channels.php');
    }
}

If you're using Laravel Passport your_middleware is auth:api

image

MateusSpadari commented 4 years ago

Thanks @happones, i used this way and works fine !

auth: { headers: { Authorization: .... } }

Cateye82 commented 4 years ago

Hello @happones !

First of all, thank you for the great work and the plugin. I'm also trying to connect and have my difficulties there, so would you please take a look?

I created a service with the following code.

const options = {
            broadcaster: 'socket.io',
            host: this.serverUrl + ':6001',
            auth: {
                headers: {
                    Authorization: 'Bearer ' + this.environmentService.user.token
                }
            },
            debug: true
        };

        this.Echo = new TnsEcho(options);

        this.Echo.connector.socket.on('connect', () => {
            console.log('online');
            this.status.next('online');
        });

        this.Echo.connector.socket.on('disconnect', () => {
            console.log('offline');
            this.status.next('offline');
        });

        this.Echo.private('message.' + this.environmentService.user.id).listen('.message.new', e => {
            console.dir(e)
            this.status.next('online');
            //this.data.next(data);
        });

So far it works fine and it is called correctly. I checked the content of the variables and the debug, which is like this:

CONSOLE LOG file:///node_modules/nativescript-socket.io/socketio.common.js:55:0: nativescript-socket.io emit subscribe [{"channel":"private-message.1","auth":{"headers":{"Authorization":"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6Ly8xOTIuMTY4LjE3OC4zNS83L3VzZXIvbG9naW4vMiIsImlhdCI6MTU3NjE1Njk4MCwiZXhwIjoxNTc2MjQzMzgwLCJuYmYiOjE1NzYxNTY5ODAsImp0aSI6InR5WDVIZUFpV2R5MmxIcE4ifQ.I_yuCkKCO6CIvTKFOZp5uv8nan_MntAig1ENckSKwqs"}}}] CONSOLE LOG file:///node_modules/nativescript-socket.io/socketio.common.js:55:0: nativescript-socket.io on connect ["/"] CONSOLE LOG file:///src/app/shared/services/socket.service.ts:31:24: online

So he is telling me, that he is online. The only problem that I have is, that somehow he is not joining the channel. I am not receiving a connection status on the server side. I would post the server log, but there is nothing to post.

I also followed your BroadcastServiceProvider instructions.

Do you have any idea or can you help me? Thank you.

EDIT: I found out that android is complaining about the "no referrer when downgrade" as the SSL is missing. Could this cause the issue?

EDIT2: I proofed that the SSL is not the issue. Still the system is not joining the private channel.

marquipaul commented 4 years ago

Hello @Cateye82 I faced the same problem by using pusher and I solved it by adding android:usesCleartextTraffic="true" on my AndroidManifest.xml since every time you subscribe on a private channel it will send an http request to your server to authenticate the user by using the authEndpoint with Authorization Bearer token.

Cateye82 commented 4 years ago

Hello @paul122297, thank you for your reply. I somehow found a solution by making a lot of changes, but couldn't identify what made it work (to post it here). This year i completely switched to socket.io as the combination of redis, laravel-echo-server and this module made me crazy. Sometimes it worked, sometimes not (not sure which part was causing the issues). This and the ability of socket.io to get messages from the client itself (laravel-echo is only one way) for direct chats made my decision. Don't get me wrong, i still think it is a good plugin but in my case i had to search for another solution.