Why does grails websockets connection fail but continue to receive messages that have been subscribed to in the browser while using an AWS ELB? - amazon-web-services

Using Grails 2.5.5 with the spring-websockets plugin 1.3.1 and tomcat 8.0.37.
While using an AWS Elastic Load Balancer, the following error is shown in the Javascript console when loading the application in the browser
WebSocket connection to 'ws://...s.com/Application/stomp/059/uyqk9cdn/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
From research it has been found that the ELB doesn't support websockets and proxying within the ELB, a third party load balancer or potentially using a new Application Load Balancer (the applications are not in a VPC so this is not an easy solution) might be required.
However, after the error, the following logging is received:
Web Socket Opened...
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000
<<< CONNECTED
version:1.1
heart-beat:0,0
user-name:admin
connected to server undefined
>>> SUBSCRIBE
id:sub-0
destination:/topic/someTopic
Messages are then received by the client fine when they are broadcast
<<< MESSAGE
destination:/topic/someTopic
content-type:application/json;charset=UTF-8
subscription:sub-0
message-id:xb71g__u-16
content-length:89
The code to initiate the websocket connection is
<r:require module="spring-websocket"/>
<r:script>
var socket = new SockJS("${createLink(uri: '/stomp')}");
var client = Stomp.over(socket);
client.connect({}, function () {
var topic = "/topic/digTicketUpdated";
console.log("Subscribing to -> " + topic);
client.subscribe(topic, function (message) {
console.log("Push Message Received From Server");
updateStatus(message.body);
});
});
</r:script>
This is taken from the grails-spring-websocket github page.
Is it possible to figure out if this is fallback that has kicked in, the websocket actually working or some other scenario. To summarise :
Is the websocket falling back to another protocol?
Is there any way to get rid of the 400 error?

Thanks to the suggestions in the comments, it was found that after the "Unexpected response code: 400" was thrown, SockJS attempted to use a long POST XHR request to emulate the web socket and it worked. This was viewable in the Network tab of Chrome Developer Tools as "xhr_streaming".
To prevent the 400 error showing in the development console, a configurable switch was implemented so web socket connections are not attempted when it is known they are not supported, such as on an AWS ELB. This was achieved by passing by removing "websocket" from the allowed protocols when instantiating SockJS :
var allowedProtocols = ['xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']
var socket = new SockJS(uri, {}, {protocols_whitelist : allowedProtocols});
var client = Stomp.over(socket);

Related

Ember JS 3.20 app with SailsJS 0.12.x backend and websocket connection

I am using sails 0.12.14 version back-end with EmberJS 3.20 front-end. I am currently dealing with a websocket connection problem in production.
Using "sails.io.js": "^1.1.0", "socket.io-client": "^1.4.5" for sails compatibility in Ember ( same goes for sails back-end ).
Both servers are on AWS. When i use app in development (localhost), everything is OK, socket is connected, messages are pushed from back-end to fron-end via socket. But when i deploy Ember app to production, i get this:
WebSocket connection to 'ws://{my-page-domain}/socket.io/?__sails_io_sdk_version=1.1.0&__sails_io_sdk_platform=node&__sails_io_sdk_language=javascript&EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
Where is this comming from and why can't ember app access sails.io ? Back-end is running just fine.
Any idea what is happening ?
Any insight whatsoever is appreciated.
edit: i have found this Sails WebSocket connection to 'ws: but allowOnlyOrigins: [''] with my s3 bucket website endpoint point has no effect on this
SOLUTION:
edit2: if anyone struggles with the same error, it was a nginx setting i was missing.
https://stackoverflow.com/a/29232687/792724

Error 503 connecting to secure WebSockets

I am trying to establish a websockets connection to a site that uses both Amazon web services and Cloudflare. Navigating to that page and viewing the traffic with Chrome developer tools shows a ton of traffic, but if I try to open a connection from JS, Java, or wscat I get a 503 error every time:
$ wscat -c wss://idex.market
error: Unexpected server response: 503
Here's the same thing using node.js:
// test.js:
const WebSocket = require('ws');
const ws = new WebSocket('wss://idex.market/');
ws.on('message', function incoming(data) {
console.log(data);
});
$ node test.js
events.js:183
throw er; // Unhandled 'error' event
^
Error: Unexpected server response: 503
Any ideas? I suspect it's related to either Cloudflare or AWS.
First, you check ws.onError() (I'm not a nodejs developer) and see if there is any errors available on server side. Second, I have had two issues with how cloudflare services handle websocket connections that I'm going to share with you.
One was that some java libraries were not able to open secure connections because their supported cipher-suite were not matching server ones.
Second was that I could not open too many websocket connections (usually more than ~500) because I was using free plan.

I am making an android application to create routes with OSMDROID and the web router.project-osrm.org can not process the request (HTTP ERROR 503)

I am making an android application to create routes with OSMDROID and the web router.project-osrm.org can not process the request (HTTP ERROR 503).
enter image description here
HTTP response off the server contains status message:
503 Service Unavailable: Back-end server is at capacity
So problem is not on your side. The server has some limitations and cannot handle your request or it is maybe not working at all. I am not familiar with the project, but you may try to ask on their mailing list or IRC.
The router.project-osrm.org is often down so you should just make your own server. It is really easy with the docker file.

Azure App Service WebJob httpclient getasync request returning 500 internal server error

I have a webjob that is triggered by storage queued and makes a web api call when a new message is received. I tested the get request in fiddler and it works, but gets 500 internal server error when the request is made via httpclient getasync within the webjob. Can a call to a web api be made within a webjob?
I tested the get request in fiddler and it works, but gets 500 internal server error when the request is made via httpclient getasync within the webjob.
As far as I know, the 500 internal server error is thrown by your Web API server-side. Please try to wrap your code with try-catch in your Web API application and find the detailed error.
Can a call to a web api be made within a webjob?
You could send http requests within the WebJob. Here is my code sample which could work well both on my side and Azure, you could refer to it.
Functions.cs
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public static async void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
{
log.WriteLine(message);
string requestUrl = "https://bruce-chen-001.azurewebsites.net/api/values";
HttpClient client = new HttpClient();
var response = await client.GetAsync(requestUrl);
log.WriteLine("responseļ¼š{0}", await response.Content.ReadAsStringAsync());
}
Result
responseļ¼š["value1","value2"]

SMTP client returning "unable to connect to remote server" - Amazon SES server

I am trying to connect to an amazon server for sending emails by editing by web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network enableSsl="true" port="587" host="email-smtp.us-east-1.amazonaws.com" password="actual password" userName="actual username"/>
</smtp>
</mailSettings>
</system.net>
These settings are being set as evidenced below:
All I am doing in code is
SmtpClient client = new SmtpClient();
client.SendCompleted += SendCompletedCallback;
string userState = _id.ToString();
client.SendAsync(msg, userState);
but the error I get back is always
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 184.73.222.29:587
Before I was using an internal mail server, but since this will be getting pushed to the amazon cloud, I will not have access to the local server. In the case of the local server, all i had to do was specify the ip address for host and it ran just fine.
There were three issues at hand:
1: The email address it was being sent from was not verified with Amazon, and neither was the recipient (verification of recipient only needed in sandbox mode)
2: The out port I was using, 587, is blocked by my company, which seems to be common practice
3: Using async can only be done when you specify the page uses async in the aspx "header"