dockerpostmanlaravel-8pusherlaravel-websockets

How may I use Postman to connect to laravel-websockets running within Docker


I want to test receiving events broadcasted from my laravel app via laravel-websockets. The app and websockets are running successfully in docker and I have supervisor keeping everything up and running. My problem is, with Postman's websocket feature, I cannot connect to the websockets server running within docker (ws://127.0.0.1:6001). I get 404.

docker-compose.yml

version: '2'

services:
  ace-contact-import:
    build:
      context: .
      dockerfile: ./docker/Dockerfile
    ports:
      - "8787:80"
      - "6001:6001"
    volumes:
      - ./:/var/www/
    tty: true
  composer:
    build:
      context: .
      dockerfile: ./docker/composer/Dockerfile
    depends_on:
      - ace-contact-import
    volumes_from:
      - ace-contact-import
    tty: true
  redis:
    image: redis:alpine
    container_name: ace-contact-import-redis
    command: redis-server --appendonly yes --requirepass "${REDIS_PASSWORD}"
    volumes:
      - ./data/redis:/data
    ports:
      - "8002:6379"
volumes:
  logvolume01: {}

websockets.php

'apps' => [
        [
            'id' => env('PUSHER_APP_ID'),
            'name' => env('APP_NAME'),
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'path' => env('PUSHER_APP_PATH'),
            'capacity' => null,
            'enable_client_messages' => false, // peer-to-peer client messages
            'enable_statistics' => false,
        ],
    ],

broadcasting.php

'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY', 'app-key'),
            'secret' => env('PUSHER_APP_SECRET', 'app-secret'),
            'app_id' => env('PUSHER_APP_ID', 'app-id'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'host' => env('LARAVEL_WEBSOCKETS_HOST', '127.0.0.1'),
                'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
                'scheme' => env('LARAVEL_WEBSOCKETS_SCHEME', 'http'),
                'encrypted' => false, // todo
                'useTLS' => env('PUSHER_SCHEME') === 'https', // todo
            ],
        ],

.env

PUSHER_APP_ID=12345
PUSHER_APP_KEY=ABCDEFG
PUSHER_APP_SECRET=HIJKLMNOP
PUSHER_APP_CLUSTER=mt1

LARAVEL_WEBSOCKETS_HOST=host.docker.internal # also tried 127.0.0.1
LARAVEL_WEBSOCKETS_PORT=6001
LARAVEL_WEBSOCKETS_SCHEME=http

# For Echo (Client side)
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Solution

  • Create a raw ws connection to your websocket instance:

    ws://localhost:6001/app/abcdef
    

    abcdef is the PUSHER_APP_KEY which should match what's in your .env file. If your port is something other than 6001, change it in the URL.

    Once connected, you may compose a message within Postman to subscribe to your channel:

    {
       "event":"pusher:subscribe",
       "data":{
          "auth":"",
          "channel":"your-channel-name-here"
       }
    }
    

    That's it! As messages are broadcast from the server, they'll appear in the messages page at the bottom of the screen.