Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Send an event via broadcasting - Multiple Apps #815

@thyagomuris

Description

@thyagomuris

I configured my websocket to accept multiple apps

MyCustomAppManager.php

<?php


namespace App\Manager;


use App\Models\User;
use BeyondCode\LaravelWebSockets\Apps\App;


class MyCustomAppManager
{
    public function all() : array
    {

        return User::all()
            ->map(function($app) {
                return $this->normalize($app);
            })
            ->toArray();
    }

    public function findById($appId) : ?App
    {
        return $this->normalize(User::where('id',$appId)->first());
    }

    public function findByKey($appKey) : ?App
    {
        return $this->normalize(User::where('id',$appKey)->first());
    }

    public function findBySecret($appSecret) : ?App
    {
        return $this->normalize(User::where('id',$appSecret)->first());
    }

    protected function normalize($user) : ?App
    {
        if(empty($user))
        {
            return null;
        }

        $app = new App( $user->id, $user->id, $user->id);
        $app->enableClientMessages(false)->enableStatistics(false);

        return $app;
    }
}

Then I changed the config/websockets.php file

    'managers' => [

        /*
        |--------------------------------------------------------------------------
        | Application Manager
        |--------------------------------------------------------------------------
        |
        | An Application manager determines how your websocket server allows
        | the use of the TCP protocol based on, for example, a list of allowed
        | applications.
        | By default, it uses the defined array in the config file, but you can
        | anytime implement the same interface as the class and add your own
        | custom method to retrieve the apps.
        |
        */

        'app' => App\Manager\MyCustomAppManager::class,
    ],

    /*
     * This class is responsible for finding the apps. The default provider
     * will use the apps defined in this config file.
     *
     * You can create a custom provider by implementing the
     * `AppProvider` interface.
     */
    'app_provider' => BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider::class,

With these changes I was able to connect to the websocket normally.

WebsocketJump::dispatch($user->id, $data);

But when I try to send an event, laravel uses the information from config/broadcasting.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Broadcaster
    |--------------------------------------------------------------------------
    |
    | This option controls the default broadcaster that will be used by the
    | framework when an event needs to be broadcast. You may set this to
    | any of the connections defined in the "connections" array below.
    |
    | Supported: "pusher", "redis", "log", "null"
    |
    */

    'default' => env('BROADCAST_DRIVER', 'null'),

    /*
    |--------------------------------------------------------------------------
    | Broadcast Connections
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the broadcast connections that will be used
    | to broadcast events to other systems or over websockets. Samples of
    | each available type of connection are provided inside this array.
    |
    */

    'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'encrypted' => true,
                'host' => env('PUSHER_APP_HOST', '127.0.0.1'),
                'port' => env('PUSHER_APP_PORT', 3030),
                'scheme' => env('PUSHER_APP_SCHEME', 'http'),
                'curl_options' => [
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ],
            ],
        ],

        'ably' => [
            'driver' => 'ably',
            'key' => env('ABLY_KEY'),
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],

        'log' => [
            'driver' => 'log',
        ],

        'null' => [
            'driver' => 'null',
        ],

    ],

];

So I get the error below.

thrown: Unknown app id 815141651165` provided.

This error returns because it is using the id that is in the config.

I would like to know how to call the event and pass the connection information in the event or send the ID to find the correct App_id.

I really don't know what to do

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions