node.js autoscaling/load balancing any methods? with socket.io - node.js

I am trying to deploy a node.js project so it can auto scale with a loadbalancer.
I have tried AWS ELB elastic beanstalk. After a much time spent, the ELB doesn't like the socket.io connect as much as expected.
From this I have searched many methods to fix this issue:
nginx proxying
changed listeners on the loadbalancer to allow TCP port 3000 (other ports too depending on the forum/question I looked at)
redis server to share socket connection between different nodes.
I can't really put all the sites that I've tried to get this working.
But it hasn't worked at all.
The only thing I probably haven't tried is using route53...
So the question is:
If there is someone that has a proper node.js app with socket.io working with elastic beanstalk (or EC2 instances with a load balancer and auto scaling) working, is there a way to do this, and what would that method be?
If there are links that have a few step by step methods that would be awesome.
Thanks in advance!

Related

Recently having trouble with socket.io connections via a Digitalocean load balancer (400 Error)

I have a DO Load Balancer that has 4 servers behind it, I've been using socket.io with Sticky Sessions enabled in the Load Balancer settings and it had been working just fine for a while.
Recently clients have not been able to connect at all getting a 400 error immediately on connection. I haven't changed anything in the way I connect to the sockets at all. If I do require that the transport be 'websocket' only from the client it does connect successfully, but then I lose out on the polling backup (one of the main benefits of socket.io).
Also, connecting directly to one of the droplets works as expected, so the issue definitely stands with the Load Balancer.
Does anyone have any idea as to any kind of set up that should be in place for this to work with the DO Load Balancers? Anything that might have changed recently?
I'm running socket.io on a NodeJS server with Express if that helps at all.
Edit #1: Added a screenshot of the LB Settings

Problem running Express app over HTTPS on aws

I have an ExpressJS backend and I want to run over https on aws (so I don't get 'mixed type content' error when trying to connect with my frontend which runs over https), it's running great using http but when using https it doesn't work.
I asked this question before and I got answers like 'use nginx', 'use load balancer', unfortunately I don't know much about this stuff as I'm not very experienced with all aws variations and options, are there any tutorials I can follow step by step ? or any easy way to serve my backend over https without complexity?
any easy way to serve my backend over https without complexity?
The easiest way (don't confused with the cheapest way) is to change your EB environment to load-balanced one. You can do this in EB console's configuration settings.
This change will create Application Load Balancer for your app, and place it in-front of your instance. Once ALB is running you can follow this AWS guide:
How can I configure HTTPS for my Elastic Beanstalk environment?
In the above, only section Terminate HTTPS on the load balancer would be relevant.
Depending on the nature of your application, is it fully dynamic, or more on static side, you could also consider using Using Elastic Beanstalk with Amazon CloudFront, instead of using ALB. CloudFront could be also be easily setup to use HTTPS between clients and CloudFront, but the issue is that traffic between CloudFront and your EB instance would go over the internet unencrypted (HTTP). Obviously, you could make it HTTPS, but this requires further changes and configurations which does not fall into category of "easy ways".

AWS ElasticBeanstalk + Socket.IO + SSL issue

I'm having trouble working with AWS ElasticBeanstalk with NodeJS environment + Socket.IO + SSL for our messaging app.
I'm already running in circles trying to fix the issue but I always ends up to a Websocket handshake error (504 GATEWAY_TIMEOUT).
AWS Elastic Beanstalk Load Balancer configuration:
ELB security inbound rule
Instance's security inbound rule
Response error
Front end code
Thanks in advance. Any help is highly appreciated!
Finally!!! Got it!
So after a lot of hours burned, this solution works for me.
The problem is that I have HTTPS in my load balancer pointed to HTTP on my ec2 instance. That causes a problem because websocket runs on TCP and not HTTP/S. The port number also did matter in my case.
Original config (not-working)
New config (working)
I hope this can be of any help in the future. :-)

amazon beanstalk tcp app not responding

i am running a nodejs tcp app at my aws linux ec2 instance . the basic code is given below
var net = require('net');
net.createServer(function(socket){
socket.write('hello\n');
socket.on('data', function(data){
socket.write(data.toString().toUpperCase())
});
}).listen(8080);
and its run like charm, but i wanted to run this same app at aws beanstalk (just to get the benefit of auto scaling). ya ya i am not aws ninja. by the way to get the public ip at beanstalk i use aws VPC.
beanstalk app connect to VPC = checked.
VPC 8080 port open = checked .
change hard coded port 8080 to process.env.PORT = checked.
but if i ping anything at port 8080 it does not return 'hello' from the application. what i am missing ?
Your application is not implementing HTTP. ElasticBeanstalk by default is going to configure the Elastic Load Balancer (ELB) to act as an HTTP load balancer. This means your instance is not healthy and is not being put into service by the ELB and the ELB itself would also be rejecting the non-HTTP request.
Important note: While it would be possible to modify ElasticBeanstalk to work for your use case, you are going to be using it in a non-standard way so there will be some risks. If you are regularly creating and deleting environments using CloudFormation or the API then you will likely run into a lot of headaches.
If you are going to just create an environment and leave it running then I suggest you take the following steps.
First off, ElasticBeanstalk's nodejs configuration is going to configure an Nginx server on the EC2 instance, since you are using TCP you will want to bypass this entirely. This can be done by re-configuring the ELB and security groups. It would be easiest to just leave Nginx running, it just will not be used, just make sure it is not on the same port as nodejs.
By default the ELB configuration will look like this:
The step you missed was updating the ELB to use TCP load balancing on the appropriate ports. You can go into the EC2 web console under Load Balancers and update the load balancer configuration for the already created Beanstalk to look like this:
You will also want to modify the health check of the load balancer to be on the correct port:
Last, double check to make sure the security groups for both the load balancer and EC2 instances allow the appropriate ports to be accessed. The last thing to check, but you already mentioned you looked, is that your VPC's NACLs also allow the appropriate ports to be accessed.

Can I use Amazon ELB instead of nginx as load balancer for my Node.Js app?

I have a Node.js app and I've seen a lot of posts here in SO that it needs to be behind a nginx as load balancer. Since I'm already accustomed to Amazon's services, thus my question.
Yes, but there are a few gotcha to keep in mind:
If you have a single server, ensure you don't return anything except 200 to the page that ELB uses to check health. We had a 301 from our non-www to www site, and that made ELB not send anything to our server because of it.
You'll get the ELB's IP instead of the client's in your logs. There is an ngx_real_ip module, but it takes from config hacking to get it to work.
ELB works great in front of a basic Node.js application. If you want WebSockets, you need to configure it for TCP balancing. TCP balancing doesn't support sticky sessions though, so you get one or the other.

Resources