ably / ably-php

PHP client library SDK for Ably realtime messaging service
https://ably.com/download
Apache License 2.0
54 stars 11 forks source link

Invalid connectionKey error when i try to broadcast an event #165

Closed faso-dev closed 2 years ago

faso-dev commented 2 years ago

Hi, i try to use ably in my php project and i get this error when i try to dispatch an event :

"message": "Malformed message; invalid connectionKey. (See https://help.ably.io/error/40006 for help.)", "exception": "Ably\\Exceptions\\AblyRequestException", "file": "***/vendor/ably/ably-php/src/Http.php", "line": 195,

in my .env file, i've this configuration and i use laravel :

backend setup : https://laravel.com/docs/9.x/broadcasting#ably frontend : https://laravel.com/docs/9.x/broadcasting#client-ably

#< ABLY SETUP
ABLY_KEY=XXX.YYYY:ZZZ
MIX_ABLY_PUBLIC_KEY=XXX.YYYY
#>

in frontend :

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_ABLY_PUBLIC_KEY,
    wsHost: 'realtime-pusher.ably.io',
    wsPort: 443,
    disableStats: true,
    encrypted: true,
});
sacOO7 commented 2 years ago

You can try setting env. variables as

ABLY_KEY=
MIX_ABLY_KEY="${ABLY_KEY}"

and access it with

    window.Echo = new Echo({
        authEndpoint: '/broadcasting/auth?pusher=1', // Force use of AblyBroadcasterDeprecated
        broadcaster: 'pusher',
        key: process.env.MIX_ABLY_KEY.split(':')[0], // Use first part of the API key before : (colon)
        wsHost: 'realtime-pusher.ably.io',
        wsPort: 443,
        disableStats: false,
        encrypted: true,
    });

As far as I remember, when you set env. variable, it needs to be accessed prefixed with MIX. So, the second variable MIX_ABLY_KEY might not be needed. You can try both ways. I am referencing code https://github.com/QSD-BiH/laravel-broacast-app/blob/feature/ably-integration/resources/js/bootstrap.js and https://github.com/QSD-BiH/laravel-broacast-app/blob/feature/ably-integration/.env.example

sacOO7 commented 2 years ago

Related to https://github.com/ably/ably-php-laravel/issues/13

faso-dev commented 2 years ago

You can try setting env. variables as

ABLY_KEY=
MIX_ABLY_KEY="${ABLY_KEY}"

and access it with

    window.Echo = new Echo({
        authEndpoint: '/broadcasting/auth?pusher=1', // Force use of AblyBroadcasterDeprecated
        broadcaster: 'pusher',
        key: process.env.MIX_ABLY_KEY.split(':')[0], // Use first part of the API key before : (colon)
        wsHost: 'realtime-pusher.ably.io',
        wsPort: 443,
        disableStats: false,
        encrypted: true,
    });

As far as I remember, when you set env. variable, it needs to be accessed prefixed with MIX. So, the second variable MIX_ABLY_KEY might not be needed. You can try both ways. I am referencing code https://github.com/QSD-BiH/laravel-broacast-app/blob/feature/ably-integration/resources/js/bootstrap.js and https://github.com/QSD-BiH/laravel-broacast-app/blob/feature/ably-integration/.env.example

Thanks for support. So i solved the issue by adding another package : https://github.com/springboardVR/Laravel-Ably-Broadcaster and it work. But, i'll try your solution also to see if the issue will be resolved.

Thanks again