Search code examples
laraveldockerdockerfilelaravel-websockets

Docker Laravel Websockets - Connection closed before receiving a handshake response


I have a Docker environment running Laravel and I want to get laravel-websockets working.

When I visit here:

http://localhost:8000/laravel-websockets

I get the following error:

Websockets error

Dev tools gives me the following:

WebSocket connection to 'ws://localhost:6001/app/local?protocol=7&client=js&version=4.3.1&flash=false' failed: Connection closed before receiving a handshake response

I've got the following docker-compose.yaml:

version: "3.8"

services:
    server:
        build:
            context: .
            dockerfile: dockerfiles/nginx.dockerfile
        ports:
            - "8000:80"
            - "6001:6001"
        volumes:
            - ./src:/var/www/html
            - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
        depends_on:
            - app
    app:
        build:
            context: .
            dockerfile: dockerfiles/php.dockerfile
        volumes:
            - ./src:/var/www/html:delegated

My php.dockerfile looks like so:

FROM php:8.0-fpm-alpine

USER root

WORKDIR /var/www/html

COPY src .

RUN apk --no-cache add libxml2-dev
RUN docker-php-ext-install soap
RUN docker-php-ext-install pdo pdo_mysql

RUN echo 'max_execution_time = 120' >> /usr/local/etc/php/conf.d/docker-php-maxexectime.ini;

EXPOSE 6001

I'm by no means an expert so I might be missing something obvious. Any help appreciated.

============

Edit: Additional info

It's set up locally:

'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' => '127.0.0.1',
            'port' => 6001,
            'scheme' => 'http'
        ],
    ],

And I've run:

php artisan websockets:serve

Solution

  • Apologies, I'm dumb.

    services:
        server:
            build:
                context: .
                dockerfile: dockerfiles/nginx.dockerfile
            ports:
                - "8000:80"
                - "6001:6001"   <<<<<<<<
    

    Port 6001 needs to be exposed on my app container, not on the server:

    services:
        app:
            build:
                context: .
                dockerfile: dockerfiles/php.dockerfile
            ports:
                - "6001:6001"   <<<<<<<<