stompjs socket connection in kubernetes - kubernetes

I'm using stompjs, sock-client to establish a socket connection from a react-app to a springboot app. Both front-end and back-end are hosted in the same kubernetes cluster, but in different namespaces. Both are load balanced.
When running locally, this works fine:
UI:
const endpoint = 'http://localhost:8080/websocket';
let socket = new SockJs(endpoint);
stompClient = StompJs.over(socket);
stompClient.connect({}, this.onStompConnectSuccess, this.onStompErrorCallback);
Server side:
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket").setAllowedOrigins("http://localhost:3000").withSockJS();
}
However what URL do i use for the hosted versions? Setting the load-balanced URLs gives an error
Whoops! Lost connection to https://load-balanced-url/websocket

Related

Flutter Socket.IO cannot connect to server, but browser works fine

Was going to use Socket.IO for authorizing in a flutter application, but it wont work on anything other than localhost:
String serverURL = "https://example.awsapprunner.com";
IO.Socket socket = IO.io('$serverURL/client', <String, dynamic>{'transports': ['websocket']});
checkUserExists(String phoneNum, context) {
socket.connect();
socket.emit('login', phoneNum);
socket.on('login_error', (res)=> {
log("new user"),
});
socket.on('session-started',(sid)=>{
log("old user!"),
});
}
The app won't even connect to the socket so anything after socket.connent() never runs.
Here's what I have tried
'autoConnect': true
replacing 'https' with 'wss' or 'ws'
removing 'https://' altogether
Different Cloud Services (AWS, GC, Azure)
local server (works and connects perfectly fine)
connecting to the server via browser (also works perfectly fine)

Strange issue with Vertx Http request

I configured an HTTPS website on AWS, which allows visiting from a white list of IPs.
My local machine runs with a VPN connection, which is in the white list.
I could visit the website from web browser or by the java.net.http package with the below code:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://mywebsite/route"))
.GET() // GET is default
.build();
HttpResponse<Void> response = client.send(request,
HttpResponse.BodyHandlers.discarding());
But if I replaced the code with a Vertx implementation from io.vertx.ext.web.client package, I got a 403 forbidden response from the same website.
WebClientOptions options = new WebClientOptions().setTryUseCompression(true).setTrustAll(true);
HttpRequest<Buffer> request = WebClient.create(vertx, options)
.getAbs("https://mywebsite/route")
.ssl(true).putHeaders(headers);
request.send(asyncResult -> {
if (asyncResult.succeeded()) {
HttpResponse response = asyncResult.result();
}
});
Does anyone have an idea why the Vertx implementation is rejected?
Finally got the root cause. I started a local server that accepts the testing request and forwards it to the server on AWS. The testing client sent the request to localhost and thus "Host=localhost:8080/..." is in the request header. In the Vert.X implementation, a new header entry "Host=localhost:443/..." is wrongly put into the request headers. I haven't debug the Vert.X implementation so I have no idea why it behaviors as this. But then the AWS firewall rejected the request with a rule that a request could not come from localhost.

404 Errors using Vert.x web proxy code taken from its documentation

I am using the Vert.x Web library to create a test application for a reverse proxy. I am using the information provided on the Vert.x site:
https://vertx.io/docs/4.1.0/vertx-web-proxy/java/#_using_vert_x_web_proxy
I have written some simple test code, based on the documentation at the above site:
public class WebTest
{
public static void main(String[] args)
{
Vertx vertx = Vertx.vertx();
HttpClient client = vertx.createHttpClient();
HttpProxy proxy = HttpProxy.reverseProxy(client);
proxy.origin(8080,"localhost");
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route(HttpMethod.GET,"/foo").handler(ProxyHandler.create(proxy));
server.requestHandler(router);
server.listen(8010);
}
}
I have a web server (the so- called "origin" in Vert.x terminology) running on Port 8080. The only difference between the code above and the code on the web site is that I am using different port numbers for the proxy and the origin server. According to the documentation on the Vert.x website, a browser going to http://localhost:8010/foo should access the origin server.
Instead, I am getting 404 Not found errors.
Is there something missing in this code? Maybe something not covered in the documentation?
Does anyone have any idea how to make it work properly?
#Factor Three you are missing backend part of the guide and also you are mixing parts in an unorganized way. Following the guide it can be like this:
Vertx vertx = Vertx.vertx();
// Origin Server (Backend 8080)---------
// Here you deploy the real server with the endpoint ("hello world" in this case)
HttpServer backendServer = vertx.createHttpServer();
Router backendRouter = Router.router(vertx);
backendRouter
.route(HttpMethod.GET, "/foo")
.handler(
rc -> {
rc.response().putHeader("content-type", "text/plain").end("hello world");
});
backendServer.requestHandler(backendRouter).listen(8080);
// Proxy Server (8010)
// Here you deploy the server that will act as a proxy
HttpServer proxyServer = vertx.createHttpServer();
Router proxyRouter = Router.router(vertx);
proxyServer.requestHandler(proxyRouter);
proxyServer.listen(8010);
// Proxy Magic
// Here you route proxy server requests (8010) to the origin server (8080)
HttpClient proxyClient = vertx.createHttpClient();
HttpProxy httpProxy = HttpProxy.reverseProxy(proxyClient);
httpProxy.origin(8080, "localhost");
proxyRouter.route(HttpMethod.GET, "/foo").handler(ProxyHandler.create(httpProxy));

flutter app can not connect to a webSocket

I am building a flutter app which needs to connect to the server and exchange data using websocket. The server is in JAVA and using SockJs and Stomp to implement this functionality.
I am using Stomp dart client and webSocket packages from pub.dev/packages.
This is the part of my code where I am trying to connect :
clientConnect() async {
String cookie = await storage.read(key: "cookie");
final stompClient = StompClient(
config: StompConfig(url: 'ws://192.168.0.13:8080/....', onConnect: onConnect,
webSocketConnectHeaders: {"cookie": cookie }));
stompClient.activate();
}
The problem I am facing is, my flutter app is not able to connect to the server and throws the this error.
WebSocketException: Connection to 'http://192.168.0.12:8080/....' was not upgraded to websocket
Late answer (maintainer of the stomp package):
Since you seem to be using StompJS on the java-side you need to use the special StompJS config in the client. Here is the example from the documentation:
https://pub.dev/packages/stomp_dart_client#use-stomp-with-sockjs
StompClient client = StompClient(
config: StompConfig.SockJS(
url: 'https://yourserver',
onConnect: onConnectCallback
)
);

How to open TCP port on localhost on VSTS build agent?

When using the online hosted version of Visual Studio Team Services, my unit tests are unable to connect to a service listening on a TCP port on the localhost of the build agent. The service is able to start and open the TCP port but it seems unreachable for the unit test.
Error message:
2017-06-20T12:05:00.8231306Z ##[error]------------
System.Net.Http.HttpRequestException : An error occurred while sending
the request. 2017-06-20T12:05:00.8231306Z ##[error]----------------
System.Net.WebException : Unable to connect to the remote server
2017-06-20T12:05:00.8231306Z ##[error]--------------------
System.Net.Sockets.SocketException : No connection could be made
because the target machine actively refused it 127.0.0.1:41670
The service that opens the TCP port is started with:
public void Start()
{
HttpPort = ObtainFreePort();
TcpPort = ObtainFreePort();
ClusterVNode node = EmbeddedVNodeBuilder.AsSingleNode()
.WithInternalTcpOn(new IPEndPoint(IPAddress.Loopback, TcpPort))
.WithExternalTcpOn(new IPEndPoint(IPAddress.Loopback, TcpPort))
.WithInternalHttpOn(new IPEndPoint(IPAddress.Loopback, HttpPort))
.WithExternalHttpOn(new IPEndPoint(IPAddress.Loopback, HttpPort))
.AddExternalHttpPrefix($"http://+:{HttpPort}/")
.RunProjections(ProjectionsMode.All)
.StartStandardProjections()
.RunInMemory()
.Build();
node.StartAndWaitUntilReady().Wait();
}
static int ObtainFreePort()
{
using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
sock.Bind(new IPEndPoint(IPAddress.Loopback, 0));
var port = ((IPEndPoint)sock.LocalEndPoint).Port;
sock.Close();
return port;
}
}
This does work on my local machine :) Is this not supported in Visual Studio Team Services online?
If you're using the hosted agent, you can't open up ports or change anything about the machine's configuration. You'll need to set up your own agent for builds.
Also, if a test requires TCP communication, it's no longer a unit test. Unit tests have no external dependencies.