0

I created containers with Docker app, nginx and websocket The connection work perfect but when I dispatch nothing happen.

Client side

        window.io = io;
        window.Echo = new Echo({
            broadcaster: "pusher",
            key: "12313",
            cluster: "mt1",
            wsHost: window.location.hostname,
            wsPort: 6001,
            forceTLS: false,
            disableStats: true,
            auth: {
                headers: {
                    Authorization: `Bearer ${token}`
                }
            }
        });

        const channel = window.Echo.private(`grant-extra-chance.${dscID}`);
        channel.listen('.Api\\Events\\GrantExtraChanceEvent', (result) => {
            sessionStorage.setItem('popUpVisible', true);
            sessionStorage.setItem('numberOfChances', result.chances);
            setNumberExtraChances(result.chances);
            setShowPopup(true);
        });

GrantExtraChanceEvent.php

<?php
namespace Api\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class GrantExtraChanceEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(private $dcsId, private $chances)
    {
        //
    }
    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel("grant-extra-chance.{$this->dcsId}");
    }

    /**
     * @return array
     */
    public function broadcastWith()
    {
        return [
            'chances' => $this->chances
        ];
    }
}

config/broadcast.php

    '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' => false,
            'host' => env('PUSHER_APP_HOST', '127.0.0.1'),
            'port' => 6001,
            'scheme' => 'http',
            'curl_options' => [
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_SSL_VERIFYPEER => 0,
            ]
        ],
    ],

.env

BROADCAST_DRIVER=pusher

PUSHER_APP_ID=123123
PUSHER_APP_KEY=123123
PUSHER_APP_SECRET=123123
PUSHER_APP_CLUSTER=mt1
PUSHER_APP_HOST=websocket

Webscoket Dockerfile:

FROM php:8.0.9-cli

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libjpeg-dev \
    libzip-dev \
    libfreetype6-dev \
    libonig-dev \
    libxml2-dev \
    mariadb-client \
    zip \
    unzip

RUN docker-php-ext-install opcache
COPY ./docker-compose/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip soap

# Allow user to execute Artisan/ Composer commands
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

EXPOSE 6001

CMD ["php", "artisan", "websockets:serve", "--host=0.0.0.0"]

App Dockerfile:

FROM php:8.0.9-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libjpeg-dev \
    libzip-dev \
    libfreetype6-dev \
    libonig-dev \
    libxml2-dev \
    mariadb-client \
    zip \
    unzip


RUN docker-php-ext-install opcache
COPY ./docker-compose/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

RUN cd /usr/local/etc/php/conf.d/ && \
  printf 'post_max_size = 200M; \n upload_max_filesize = 200M;' >> /usr/local/etc/php/conf.d/docker-php-uploads.ini

RUN docker-php-ext-configure gd --with-freetype --with-jpeg

# Get phpredis
ENV REDIS_VERSION 4.0.2

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip soap

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Install xdebug, by default it's enable, for disable or enable run this command './xdebug.sh disable' or './xdebug.sh enable'
RUN pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "changed-xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.client_port=9003" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.discover_client_host=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.client_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

# Allow to run all commands from xdebug.sh
RUN mkdir -p /usr/local/etc/php/disabled
RUN chown $user /usr/local/etc/php/disabled
RUN chown $user /usr/local/etc/php/conf.d

# Allow user to execute Artisan/ Composer commands
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

USER $user

docker-compose.yml

version: "3.3"
services:
  app:
    build:
      args:
        user: www-data
        uid: 1000
      context: ./
      dockerfile: Dockerfile
    image: fullapp
    extra_hosts:
      - "host.docker.internal:host-gateway"
    container_name: fullapp-app
    restart: unless-stopped
    working_dir: /var/www/
    environment:
      - COMPOSER_MEMORY_LIMIT=-1
      - CHOKIDAR_USEPOLLING=true
      - DEBUG=1
    volumes:
      - ./:/var/www
    networks:
      - fullapp

  adminer:
    build: ./docker-compose/adminer
    container_name: fullapp-adminer
    restart: always
    ports:
      - 8882:8080
    networks:
      - fullapp
    depends_on:
      - db

  db:
    image: mysql:5.7
    container_name: fullapp-db
    ports:
      - 3392:3306
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: fullapp
      MYSQL_ROOT_PASSWORD: fullapp
      MYSQL_PASSWORD: fullapp
      MYSQL_USER: fullapp
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ~/fullapp/mysql-data:/var/lib/mysql
#      This line you could uncomment if you have .sql file for import on first run container and first path is path where is located your file path in PC
#      - ./club_pwa_app.sql:/docker-entrypoint-initdb.d/dump.sql
    networks:
      - fullapp

  nginx:
    image: arm64v8/nginx
    container_name: fullapp-nginx
    restart: unless-stopped
    ports:
      - 9992:80
    volumes:
      - ./:/var/www
      - ./docker-compose/nginx:/etc/nginx/conf.d/
    networks:
      - fullapp

  websocket:
    build:
      dockerfile: ./docker-compose/websocket/Dockerfile
      context: ./
    restart: unless-stopped
    volumes:
      - ~/fullapp/websocket-data:/data
      - ./:/var/www
    ports:
      - 6001:6001
    depends_on:
      - redis
    networks:
      - fullapp

  redis:
    build: ./docker-compose/redis
    restart: unless-stopped
    volumes:
      - ~/fullapp/redis-data:/data
    expose:
      - "6379"
    #    ports:
    #      - "${REDIS_PORT}:6379"
    networks:
      - fullapp

networks:
  fullapp:
    driver: bridge

I run dispatch on tinkerwell but nothing happen

enter image description here

I expect to catch on listening GrantExtraChanceEvent with json {"chances": 1}

Ion
  • 39
  • 3
  • 13

0 Answers0