Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

madprabh's avatar

Laravel websockets deployment behind AWS ALB

Hey Folks,

So I have a laravel app deployed behind a AWS application load balancer. I am using the open-source laravel websockets in place of pusher which is also deployed on the same EC2 instance.

How do I get my app to connect to this websocket over the ALB?

Anyone any clues?

0 likes
1 reply
LaryAI's avatar
Level 58

Deploying Laravel Websockets behind an AWS Application Load Balancer (ALB) requires a few steps to ensure that the websockets are properly routed and handled by the ALB. Here's a step-by-step guide to help you set it up:

  1. Configure the Target Group:

    • Create a new target group in the AWS EC2 console.
    • Set the target type to "instance" or "IP" depending on your setup.
    • Specify the protocol as "HTTP" or "HTTPS" (if you have SSL configured).
    • Set the port that your websocket server is listening on (e.g., 6001).
  2. Register Targets:

    • Register the EC2 instance(s) that are running the Laravel Websockets server with the target group you just created.
  3. Configure the Listener:

    • Go to the ALB and add a new listener for the port you want to use for the websockets (e.g., 80 or 443 for HTTP/HTTPS).
    • Add a rule to forward requests to the target group you created for the websocket connections.
    • If you're using HTTPS, ensure you have the correct SSL certificate attached to the listener.
  4. WebSocket-Specific Listener Rule:

    • You may need to add a specific listener rule to route WebSocket traffic based on a path pattern or hostname. For example, you could route traffic with a path pattern of /ws to your WebSocket server.
  5. Update Security Groups:

    • Ensure that the security group attached to your ALB allows inbound traffic on the WebSocket port from the necessary IP ranges.
    • Also, ensure that the security group for your EC2 instances allows inbound traffic on the WebSocket port from the ALB.
  6. Configure Laravel Websockets:

    • In your config/websockets.php file, make sure you set the host to 0.0.0.0 to allow connections from outside the server.
    • Ensure the port matches the one you've configured in your target group.
  7. Client-Side Configuration:

    • When initializing the WebSocket connection in your frontend code, use the ALB's DNS name and the correct port. If you're using Laravel Echo, it might look something like this:
import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your-pusher-key',
    wsHost: window.location.hostname,
    wsPort: 6001, // Use the port you've set up for the WebSocket connections
    disableStats: true,
    encrypted: true // Set to true if using wss (WebSocket Secure)
});
  1. Testing:
    • After configuring everything, test the WebSocket connection to ensure that it's working correctly through the ALB.

Remember that when using WebSockets with an ALB, AWS only supports WebSockets on HTTPS listeners (port 443) with an SSL certificate. If you're using HTTP, you might need to switch to a Network Load Balancer (NLB) which supports both TCP and UDP traffic.

Also, keep in mind that AWS ALB has a timeout for idle connections which might affect long-standing WebSocket connections. You may need to handle pings/pongs within your application to keep the connection alive.

Please or to participate in this conversation.