0

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}"
user3389171
  • 141
  • 3
  • 17

1 Answers1

5

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.

user3389171
  • 141
  • 3
  • 17
  • If I duplicate the postman websocket request and then connect using the above method and subscribe to `"your-channel-name-here"` how to I send a message that it is received on previously connected-subscribed instance. BTW, Thanks a lot for this answer help a lot in debugging ws. – karmendra Nov 24 '22 at 11:16
  • @karmendra Postman will allow you to establish 2 separate connections to your websocket server with no issues. You may run into problems trying to connect 2 tabs to the same private channel AFAIK. See the "Defining broadcast events" example on how you would send a message from your websocket application to a particular user: https://laravel.com/docs/8.x/broadcasting#defining-broadcast-events – user3389171 Nov 25 '22 at 15:57
  • What do you put in the `auth` field in case you want to connect to a private channel – Edgar Feb 07 '23 at 00:13
  • Never mind, I figured out how to authenticate private channels – Edgar Feb 07 '23 at 10:35
  • It'd be nice to share it @Edgar – leevanoetz Aug 25 '23 at 12:53