Sails.js Socket Issue on Google App Engine - node.js

I'm running Sails.js (v0.11.0) on a Managed VM in Google App Engine, everything works fine provided I disable sockets. I am using the default Sails project, with one model (User) with no custom attributes and am simply visiting the home page in my browser. I see the following in Chrome's console window;
WebSocket connection to 'ws://localhost:8080/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=websocket&sid=GShPZd_tjzqnrigNAAAA' failed: Error during WebSocket handshake: 'Upgrade' header is missing
sails.io.js:143
|> Now connected to Sails.
\___/ For help, see: http://bit.ly/1DmTvgK
(using browser SDK #v0.11.0)
4(index):1 Font from origin 'http://sailsjs.org' has been blocked from loading by Cross-Origin Resource Sharing policy: The 'Access-Control-Allow-Origin' header contains the invalid value ''. Origin 'http://localhost:8080' is therefore not allowed access.
sails.io.js:4 POST http://localhost:8080/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…ascript&EIO=3&transport=polling&t=1427404282042-3&sid=GShPZd_tjzqnrigNAAAA 503 (Service Unavailable)
sails.io.js:143 Failed to connect socket (probably due to failed authorization on server) Error: Error: xhr post error {type: "TransportError", description: 503, stack: (...), message: "xhr post error"}
sails.io.js:143 ====================================
sails.io.js:143 Socket was disconnected from Sails.
sails.io.js:143 Usually, this is due to one of the following reasons:
-> the server was taken down
-> your browser lost internet connectivity
sails.io.js:143 ====================================
sails.io.js:143
Socket is trying to reconnect to Sails...
_-|>_- (attempt #1)
sails.io.js:4 POST http://localhost:8080/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…ascript&EIO=3&transport=polling&t=1427404302073-4&sid=GShPZd_tjzqnrigNAAAA 503 (Service Unavailable)
sails.io.js:4 GET http://localhost:8080/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1427404303080-5 503 (Service Unavailable)
'Upgrade header is missing' seems like it may be the culprit, but I don't understand how the headers could have been changed from whatever Socket.io requires, I have an app.yaml file that simply allows passes all traffic to sails.
#app.yaml
module: default
version: 1
runtime: custom
api_version: 1
vm: true
manual_scaling:
instances: 1
handlers:
- url: /.*
script: app.js
Any help greatly appreciated!
Cheers! R.

I don't think you're going to be able to use Sails' web sockets out of the box. Google App Engine is not something I'm familiar with, but it appears you need to use their API for web sockets. See this example:
https://github.com/thebergamo/appengine-websocket-nodejs
After doing some more poking around I eventually came across this post, which is pretty much a duplicate of this one and should give you some more clues. The short answer is you need to have a separate Google Managed VM running your web socket service and then you access it directly via ip address, which seems to take all the fun out of using Sails.
Run Websocket on GAE

Related

Why is my HttpPlatformHandler server app getting HTTP Error 502.3 Bad Gateway

I'm writing a HttpPlatformHandler server app with C++ and Winsock functions. It works great on my development machine but when deployed on other machines it fails. I can run it separately and access it from the browser using the default port I coded into it on those other machines. Here are the error details:
Web Browser:
HTTP Error 502.3 Bad Gateway
Error Code 0x80070057
There was a connection error while trying to route the request.
Event Viewer:
HttpPlatformHandler
Process '0' failed to start. Port = 19516, Error Code = '-2147024809'.
I figured it out. I had the web.config file in a subfolder under C:\inetpub\wwwroot with the file in the URL. I moved it to C:\inetpub\wwwroot and now it works.

Node.js Server Response Error sometime (on High load)

The nodejs server response error message on high load.
Some of the errors are:
Http failure response for (api url):0 Unknown Error and cors error. On the browser console.
On Android side the error is unexpected end of stream on Connection {api url:80, proxy=Direct hostaddress=apiurl/serverip:80 cipherSuite=none protocol=http/1.1
We have 16 GB ram 4vCPUs aws instance
nginx rever proxy to loadbalance between 4 instance of node (created through pm2), ip_hash (as we are using socketio)
while looking through the web some suggestions were "Its cors error", "it server side error, contact the backend team"
for cors We am using cors package from npm. app.use(cors()). I am looking for the server side fixation. we are using express.
since the website and app works perfectly while its working, and we get cors error during the problem. it may not be the cors error. during the high load, we get the error instantly, not time out error.
We could not figure out the issue. any suggestions?
Update: Since there are voting going on to close this, I may not get the answer, but here are some errors, Please note that the app and website (angular) works fine but got these error during pick hours, I expect timeout or node to stop, but not these errors so, thought something is wrong.
Update2: While looking though the nginx error log, we get this error.
2021/03/09 17:10:25 [alert] 1058#1058: *2946806 768 worker_connections are not enough while connecting to upstream, client: 120.xx.xxx.114, server: api.mydomain.com, request: "GET /Image-1611905660330.jpg HTTP/1.1", upstream: "http://127.0.0.1:8000/Image-1611905660330.jpg", host: "api.mydomain.com"
Update 3: Looking for the above error, found suggestion that, we need to increase worker_connections, so we update it on /etc/nginx/nginx.conf;. Now we get error *2908 socket() failed (24: Too many open files) while connecting to upstream,
Update 4: Adding worker_rlimit_nofile 20000; to the /etc/nginx/nginx.conf seems to fix the error on update 3.
The cors error is emit by the browser, not the server. server just gives browser cors policy response. Whether it should be treated as error or not is decided by the browser itself.
I'm not android developer, but i think some of your problem is made by misused request from android client, not server.
Android connect Node.js REST unexpeceed end of stream
java.io.IOException: unexpected end of stream on Connection in android

Google Cloud Node.js (502 Bad Gateway) without any errors in logs

I got (502 Bad Gateway nginx) from google cloud after I uploaded my node.js code to the app engine. I have no errors in my logs. I have done a lot of searching online and nothing helped, but I read some article saying that because the response time is long, the server returns (502 Bad Gateway).
This is my listening code:
const server = app.listen(8080, () => {
const host = server.address().address;
const port = server.address().port;
console.log("EDU-vents server started");
});
This is my app.yaml:
runtime: nodejs
env: flex
my package.json includes a start script
Thank you in advance.
Typically, when you encounter 5.x.x (502, 503, etc) it is recommended to wait a minute and try the request again. You may find more information about these errors specific to App Engine flexible environment.
However, most of the time the error code 502 with "BAD_GATEWAY" indicates that GAE terminated the application because it ran out of memory. By default, GAE Flex only has 1GB of memory and only 600MB is available for the application container. The following documentation describes steps on how to troubleshoot this type of error (You will have to most likely investigate your Stackdriver logs.
I recommend you to specify a higher CPU and Memory of your instance. If specifying a higher CPU and Memory doesn't work. I recommend you to check the Nginx error logs by following. Before SSHing to the VM instance, you need to enable debug mode for a VM instance
If this is not the case, do you have any support package/free trial? I'd recommend you to open a support ticket directly in their support center, if not you can contact Google through Google Issue Tracker as 5XX errors can be caused by different reasons.

Vue Js public Websocket blocked by CORS policy

I've created a application with Vue and Node js but I get a trouble "CORS" when I try to connect to a public websocket from "http://localhost".
Project structure
Error log
I believe the cause of this problem is a trying to connect via http to a https server.
Any idea to solve this problem in localhost ?
Try installing this node package named cors-server that will start a web server on a given port that will proxy any request received and add CORS headers to it.
To start the server simply call it from the command line and supply a port number
cors-server <port>
After that, any requests sent to http://localhost:[port] will have Access-Control-Allow-Origin: * on the response.
e.g:
POST http://localhost:3005/http://www.google.com
Hope this helps!.
The real problem in this case, is to try to connect to other domain from the client side and the server do not allow "Access-Control-Allow-Origin".
The best solution for this question is follow the arquiteture of project bellow:
https://github.com/Tucsky/SignificantTrades
In resume, implement conection websocket in server side and expose a local websocket in the client side.
In my case, Node and Vue js.

Websocket server running fine but cannot connect from client (what url should I use?)

OK this is very simple to anyone who's used websocket and nodejs.
I have created a websocket server named ws_server.js and put it in C:\Program Files (x86)\nodejs where I have installed the nodejs framework. I started the server and it is running and it says it's listening on port 8080. So far so good, I have the server running.
Now I simply want to connect to it from client code so that I can do all that lovely stuff about capturing events using event listeners etc. The problem is, embarassingly, I cannot figure out what URL to use to connect to my websocket server.
function init() {
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket("ws://localhost:8080/"); // WHAT URL SHOULD BE USED HERE?
websocket.onopen = function(evt) { alert("OPEN") };
websocket.onclose = function(evt) { alert("CLOSE") };
websocket.onmessage = function(evt) { alert("MESSAGE") };
websocket.onerror = function(evt) { alert("ERROR") };
}
function doSend(message) {
// this would be called by user pressing a button somewhere
websocket.send(message);
alert("SENT");
}
window.addEventListener("load", init, false);
When I use ws://localhost:8080 the only events that trigger are CLOSE and ERROR. I cannot get the client to connect. I must be missing something very simple. Do I need to set up my nodejs folder in IIS for example and then use that as the URL?
Just to reiterate, the websocket server is running fine, I just don't know what URL to use to connect to it from the client.
EDIT: The websocket server reports the following error.
Specified protocol was not requested by the client.
I think I have got it working by doing the following.
var websocket = new WebSocket("ws://localhost:8080/","echo-protocol");
The problem being that I needed to specify a protocol. At least now I get the onopen event. ...if nothing much else
I was seeing the same error, the entire web server goes down. Adding the protocol fixes it but leaves me wondering why it was implemented this way. I mean, one bad request should not bring down your server.
You definitely have to encase it a try/catch, but the example code provided here https://www.npmjs.com/package/websocket (2019-08-07) does not. This issue can be easily avoided.
I just wanted to share a crazy issue that I had. I was able to connect to a websocket of an old version of a 3rd party app in one computer, but not to a newer version of the app in another.
Moreever, even in new computer with the new version of the app, The app was able to connect to the websocket, but no matter what I did, when I tried to connect with my own code, I kept getting the error message that the websocket connection failed
Long story short, They changed an apache configuration that allowed connecting to the websocket via a proxy.
In the old version, apache config was:
ProxyPass /socket/ ws://localhost:33015/ retry=10
ProxyPass /socket ws://localhost:33015/ retry=10
In the new version, apache config was changed to:
ProxyPass /socket/ ws://localhost:33015/ retry=10
By bad luck, I was trying to connect to ws://localhost/socket and not to ws://localhost/socket/. As a result, proxy was not found, and connection returned an error.
Moral of the story: Make sure that you are trying to connect to a websocket url that exists.
For me, the solution was to change the URL from ws:// to wss://. This is because the server I was connecting to had updated its security, and now only accepted wss.

Resources