Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (5.6k points)
I have an Elastic Beanstalk application that was initially configured to use a Classic Load Balancer, but it's throwing errors while connecting via WebSocket. So because of this reason, I configured the application to use an Application Load Balancer, but still getting the same error when I'm trying to connect to my ALB via WebSocket. There is no guide for setting up an ALB to support WebSocket.

1 Answer

0 votes
by (12.4k points)

We can get WebSockets working with the Application Load Balancer, for that:

First, create a new Target Group for your ALB, This target Group should use the same port as your application and will need to have health checks configured.

Then, add a new Listener Rule to your ALB and this rule must have a Path to route the WebSocket setup -- / Also, set the Target Group Name to the Target Group that you created just now.

I'm using Node/Hapi/ for my server, basic setup is:

const hapi = require('hapi');

const websocket = require('./WebSocket');

var server = new hapi.Server();



where WebSocket.js is:

var io = null;

module.exports = {

    Initialize: function (http) {

        io = require('')(http);

        io.on('connection', function (socket) {

            console.log('Websocket ' + + ' connected.');

            socket.on('disconnect', function () {

                console.log('Websocket ' + + ' disconnected.');





I'm using Angular 1.5x for my client, with the It is important to configure the WebSocket client option as follows, or you will not be able to connect.

(function () {

    'use strict';


        .module('XXXXX', [])


    runHandler.$inject = ['WebSocketService'];

    function runHandler(WebSocketService) {




The WebSocket service:

(function () {

    'use strict';



        .factory('WebSocketService', WebSocketService);

    WebSocketService.$inject = [];

    function WebSocketService() {

        var socket = null;

        function initialize() {

            var url = 'http://' + ALB_URL + ':5800';

            socket = io(url, {transports: ['websocket'], upgrade: false});

            socket.on('connect', function () {

                console.log('Socket connected');


            socket.on('disconnect', function () {

                console.log('Socket disconnected');



        return {

            Initialize: initialize




Want to become AWS Expert? Come & join AWS Certification.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers


108k users

Browse Categories