Error during WebSocket handshake on AWS instance - node.js

I created a new instance on AWS (Ubuntu) and setted up the app correctly. Everything is fine except socket.io cannot connect using websockets and that kind of creates a lot of lag on mobile phones.
I get this error:
WebSocket connection to <url>' failed: Error during WebSocket handshake: Unexpected response code: 400
This happens only on my EC2 instance. I'm not using any elastic beanstack. Plain simple AWS instance running expressjs server listening on port 80. I read somewhere that nginx is required to be configured. But all I did was fired node server so there is no /etc/nginx directory on the ubuntu instance.
I've been trying to figure this out from very long time. Please help.

Related

Socket IO - WSS Throwing error -- remote client, local server

Hi I've been trying to figure this out for some time now. It's kind of hard to explain... Ill try and give some background : I'm trying to create a pwa that can work on WAN and LAN for when offline. ---- So I have a client and server, if I host the client and server both locally I'm having no issues I can connect to one another directly and everything, the socket.io and web socket work great. If I host the client and server both on Heroku they work great as well. The problem comes when I host the client on Heroku and the server locally -- which in the application I'm working on I need this. Im able to still connect to the other user if im using the WAN connection portion but if I try to make the connection using LAN it throws errors. I'm not sure what is going on, I'm still fairly new socketio, websockets, etc... ---- Here is the error I am getting Error Image. Here are my imports on the server - imports-server, how I'm setting up the server - server setup, how I set up the connection on the client -- client setup. Sorry if I wasn't clear on my explanation, thank you for help.

Heroku redis remote connection

I have had the redis instance working for some time but now i'm trying to connect to my Redis addon and i'm getting a timeout error while connecting.
My app is made with nodejs, using redis module and i have already tried with process.env.REDIS_URL and with the full url and it's not connecting
Anyone knows if Heroku addons allow remote connection? even with redis -h URL is not working

Getting 503 using websockets on heroku, Custom Domain

I'm trying to establish a websocket connection on my heroku node server but keep getting the following error:
browser.js:13 WebSocket connection to 'ws://www.mywebsite.com/' failed: Error during WebSocket handshake: Unexpected response code: 503
Works great locally, but when I deploy it to heroku, it's a no go.
I'm running a custom domain in front of my heroku instance and I'm wondering if the dns or something might be causing issues.
Anyone experience something similar?

Deploying a socket.io app in on multiple-nodes

I have an app on heroku that uses socket.io for server-client communication. Everything is working just fine. However, once I scale my app to more than 1 dyno, I get several http request errors:
can't establish a connection to the server at wss://***/socket.io/?EIO=2&transport=websocket&sid=Hky6IHdckNADdU_tAACm. socket.io.js:4520
The connection to wss://***/socket.io/?EIO=2&transport=websocket&sid=Hky6IHdckNADdU_tAACm was interrupted while the page was loading. socket.io.js:4520
can't establish a connection to the server at wss://***/socket.io/?EIO=2&transport=websocket&sid=kWymv6ItJHBcUybZAAAA. socket.io.js:4520
The connection to wss://***/socket.io/?EIO=2&transport=websocket&sid=kWymv6ItJHBcUybZAAAA was interrupted while the page was loading. socket.io.js:4520
As well as a load of
HTTP status 400
{ code: 1, message: "Session ID unknown" }
My socket.io is using the redis adapter, so the state should be shared correctly. I kind of verified this by connecting to redis and issuing the following command:
PSUBSCRIBE socket.io#*
Since I can see data traveling back and force on this channel, I am assuming my socket.io redis adapter is working fine.
Anyone know how to make socket.io work on heroku with more than 1 dyno?
You need sticky load balancing. Socket.io has a great article on it, so I'll leave it to them to explain the topic:
https://socket.io/docs/using-multiple-nodes/

Setting up a websocket capable application on an azure ubuntu vm

Okay, so I have created an ubuntu vm in the azure cloud, I have successfully launched nodejs, redis, socket.io, Express and all the components for a game I am writing.
I have setup azure endpoints on internal and external port 8080, and use app.listen(8080) in my js code.
When I browser to http://< app-name >.cloudapp.net:8080/ I can view the result of my nodejs application fine, it displays the html, css etc.
However when it attempts to start a socket connection (using socket.io) I get only this (on my chrome dev console under websockets):
HeadersPreviewResponseWebSocket Frames
Request URL:ws://<app-name>.cloudapp.net:8080/socket.io/1/websocket/13510496541533398587
Request Method:GET
Status Code:101 Switching Protocols
Request Headersview source
Connection:Upgrade
Host:<app-name>.cloudapp.net:8080
Origin:http://<app-name>.cloudapp.net:8080
Sec-WebSocket-Extensions:x-webkit-deflate-frame
Sec-WebSocket-Key:y5vueHE66phl70gle7KCtw==
Sec-WebSocket-Version:13
Upgrade:websocket
(Key3):00:00:00:00:00:00:00:00
Response Headersview source
Connection:Upgrade
Sec-WebSocket-Accept:dn+2lA6sMIXHLEmDS/Q4j/IIwxI=
Upgrade:websocket
(Challenge Response):00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
It will also crash the nodejs app on azure when i try send anything using socket.emit() on the client (browser); in my azure ssh, it just says 'DEBUG: Program node app.js exited with code null' which isn't particularly helpful.
So it obviously isn't connecting properly? I have also tried in my nodejs listening on port 80, but then I don't even get the standard webpage (html,css,etc).
From what I have read online, it is very possible to get sockets working providing your not using the web role (which I assume the vm is not).
Any idea how I can get this to work? (and preferably on port 80)?
Edit: starting to wonder if this has nothing todo with sockets, realized I get the same switching protocol message, on my home vm (where it is working). Spin off question: How can I view crash details for a nodejs program on a ubunu azure vm?
There is a time out of around 60s on the Windows Azure loadbalancer.
Does it work if you send a message back to the client just after the websocket connection is established?
If it is the case you will have to implement some keep a live message send every minutes.

Resources