Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have an Elastic Beanstalk application which was initially configured to use a Classic Load Balancer. I found that this caused errors when connecting via WebSocket. Because of this, I configured the application to use an Application Load Balancer instead, because I was told that ALBs support WebSockets. However, it seems that they do not: I get exactly the same error when attempting to connect to my ALB via WebSocket.
Do ALBs actually support WebSocket? The AWS documentation is contradictory on this.
This page
says it only supports HTTP and HTTPS. There are no guides to setting up an ALB to support WebSocket.
–
–
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 (Application load balancer) supports websockets. you can check here
also ALB close the connection depending upon idle time out configured.
SnapShot from the above blog
–
In terms of nodejs
When we use socket io on the client-side and call socket.js library
like this
const socket = io('domain.com');
what I observed is when I specified Http as protocol then it redirected the call to ws://domain.com but when I specified https then it redirected to wss://domain.com and I was getting this error failed: Error during WebSocket handshake: Unexpected response code: 400
what I did is I specified
const socket = io(window.location.origin, {reconnect: true,transports: ['websocket']});
which indeed solved my problem
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.