Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using laravel websockets package in docker environment

Use case n° 1:

steps

  • installing the package in the same container of the laravel project
  • configuring the pusher settings in .env & boradcasting.php like so
PUSHER_APP_ID=123456789
PUSHER_APP_KEY=AZERTYUIOP
PUSHER_APP_SECRET=QSDFGHJKLMWXCVBN
PUSHER_APP_CLUSTER=mt1
PUSHER_APP_HOST=localhost # or 127.0.0.1
'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'),
                'port' => 6001,
                'scheme' => 'http',
            ],
        ],
  • Exposing port 6001 in Dockerfile of project container.
  • using the package by running php artisan websockets:serve from within the container.
  • testing the package by dispatching WebSocketsTestEvent event from a tinker session
class WebSocketsTestEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    protected $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('test');
    }
}

Use case n° 2:

steps

  • installing the package in the same container of the laravel project
  • configuring the pusher settings in .env like so :
PUSHER_APP_ID=123456789
PUSHER_APP_KEY=AZERTYUIOP
PUSHER_APP_SECRET=QSDFGHJKLMWXCVBN
PUSHER_APP_CLUSTER=mt1
PUSHER_APP_HOST=ssms-app # or the @IP of the conatiner
  • creating a Dockerfile for websockets container like so:
FROM php:7.3.3-alpine

RUN apk add --no-cache \
    wget \
    curl \
    # git \
    shadow \
    build-base \
    autoconf \
    # hiredis \
    libxml2-dev \
    zlib-dev \
    libevent \
    libevent-dev \
    openssl-dev \
    gmp-dev \
    icu-dev

RUN docker-php-ext-install \
        pcntl \
        mbstring \
        pdo \
        pdo_mysql \
        tokenizer \
        xml \
        sockets \
        gmp \
        bcmath \
        intl

        # Libevent
RUN pecl install event

RUN apk update

# Cleanup
# apk del .build-deps && \
RUN rm -rf /var/cache/apk/* && \
    rm -rf /tmp/*

RUN mkdir -p /var/www

RUN groupmod -g 1000 www-data && \
    usermod -u 1000 www-data

USER www-data

WORKDIR /var/www/html

EXPOSE 6001

CMD ["php", "artisan", "websockets:serve"]
  • configuring docker-compose.yaml like so:

  ssms-app:
    build:
      context: ./docker/app
      args:
        uid: ${UID}
    container_name: ssms-app
    environment:
      - APACHE_RUN_USER=#${UID}
      - APACHE_RUN_GROUP=#${UID}
    volumes:
      - .:/var/www/html
    ports:
      - ${HOST_PORT}:80
      # - 6001:6001
    links:
      - ssms-redis
      - ssms-sockets
    networks:
      backend:
        aliases:
          - ssms-app

  ssms-sockets:
    build:
      context: ./docker/websockets
    container_name: ssms-sockets
    volumes:
      - .:/var/www/html
    ports:
      - 6001:6001
    networks:
      backend:
        aliases:
          - ssms-sockets

Observations:

  • in both cases i get the same issues:

    • the graph in the dashboard, after visiting http://localhost:8080/laravel-websockets is not displayed.

      Note: this is fixed by setting 'perform_dns_lookup' => true in config/websockets.php.

    • no traces indicating that the event is broadcasted by the package in the dashboard.

    • besides that, when using event creator from the dashboard I get 422 status code in the console
    • many developer encountered the same issue when using it with docker environment, but some of the use cases worked for ones but not for others

At last, Any solutions? I'm stuck for about 3 days

like image 543
joe_inz Avatar asked Sep 27 '19 09:09

joe_inz


1 Answers

maybe try to edit the .env file to use host.docker.internal like so

PUSHER_APP_ID=123456789
PUSHER_APP_KEY=AZERTYUIOP
PUSHER_APP_SECRET=QSDFGHJKLMWXCVBN
PUSHER_APP_CLUSTER=mt1
PUSHER_APP_HOST=host.docker.internal

after than run php artisan config:cache

like image 111
Hussein Avatar answered Oct 22 '22 04:10

Hussein