Does an Application Load Balancer support WebSockets?

只谈情不闲聊 提交于 2019-11-28 04:45:32
programmerj

I was able to get WebSockets working with the new Application Load Balancer (ALB).

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. However, the main difference is that you must enable Stickiness.

Next, add a new Listener Rule to your ALB. This rule must have a Path to route the WebSocket setup -- /socket.io. Also, set the Target Group Name to the Target Group you just created.

I am using Node/Hapi/Socket.io for my server (running on instance derived from Amazon Linux AMI). Basic setup is:

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

var server = new hapi.Server();
server.connection(config.Application);
websocket.Initialize(server.listener);

where WebSocket.js is

var io = null;

module.exports = {

    Initialize: function (http) {

        io = require('socket.io')(http);

        io.on('connection', function (socket) {
            console.log('Websocket ' + socket.id + ' connected.');

            socket.on('disconnect', function () {
                console.log('Websocket ' + socket.id + ' disconnected.');
            });
        });
    }
};

I am using Angular 1.5x for my client, with socket.io-client. It is important to configure the WebSocket client options as follows, or you will not be able to connect.

(function () {

    'use strict';

    angular
        .module('XXXXX', [])
        .run(runHandler);

    runHandler.$inject = ['WebSocketService'];

    function runHandler(WebSocketService) {
       WebSocketService.Initialize();
    }
})();

The WebSocket service:

(function () {

    'use strict';

    angular
        .module('XXXXX')
        .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
        };
    }
})();

Application load balancer supports websocket. But No support for websocket health check till 23 Feb 2017. They may add an option later. You need to set up a HTTP or HTTPS health check for your target group when you want to use a websocket behind Application Load Balancer.

From AWS document: "Note that health checks do not support WebSockets."

Reference: http://docs.aws.amazon.com/elasticloadbalancing/latest/application/target-group-health-checks.html

ALB support Websocket but the load balancer can close the connection if the instance doesn't send some data at least every "idle timeout" seconds.

ALB (Application load balancer) supports websockets. you can check here https://aws.amazon.com/blogs/aws/new-aws-application-load-balancer/enter image description here

also ALB close the connection depending upon idle time out configured.

SnapShot from the above blog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!