How to use a domain instead of public IP when connecting to SocketIO server on GCP App Engine?

742 Views Asked by At

I have my Java Spring backend deployed to GAE flexible environment. I'm using netty-socketio for hosting a socketIO server. I'm able to connect with socket.io-client using the public IP adress provided for the instance that is listed here:

enter image description here

And this is how I'm connecting to it:

const io = require("socket.io-client");

const socket = io("ws://<IP adress>:<PORT>", {
    reconnectionDelayMax: 10000,
    query: {
      "deviceId": serial
    }
});
  
socket.on("connect", () => {
    console.log("Connected. Session ID: " + socket.id);
});

Please note that netty-socketio only supports version 1.0+ so I'm using socket.io-client 1.7.4 which is not the latest version.

I'm also using a custom domain. My problem is that I'm only able to connect to the server using the IP adress but when I'm using the domain name I'm having a timeout error:

socket.io-client:url parse ws://<DOMAIN>:<PORT> +0ms
  socket.io-client new io instance for ws://<DOMAIN>:<PORT> +4ms
  socket.io-client:manager readyState closed +3ms
  socket.io-client:manager opening ws://<DOMAIN>:<PORT> +0ms
  socket.io-client:manager connect attempt will timeout after 20000 +48ms
  socket.io-client:manager readyState opening +6ms
  socket.io-client:manager connect attempt timed out after 20000 +20s
  socket.io-client:manager connect_error +2ms
  socket.io-client:manager cleanup +1ms
Connect error: timeout
  socket.io-client:manager will wait 868ms before reconnect attempt +2ms
  socket.io-client:manager attempting reconnect +870ms
  socket.io-client:manager readyState closed +0ms
  socket.io-client:manager opening ws://<DOMAIN>:<PORT> +1ms
  socket.io-client:manager connect attempt will timeout after 20000 +4ms

This is how I'd like to connect:

const io = require("socket.io-client");

const socket = io("ws://<DOMAIN>:<PORT>", {
    reconnectionDelayMax: 10000,
    query: {
      "deviceId": serial
    }
});
  
socket.on("connect", () => {
    console.log("Connected. Session ID: " + socket.id);
});

The things I did to make it work so far:

  • I'm having a network session in the app.yaml with the following configuration:
network:
  session_affinity: true
  forwarded_ports:
    - <PORT>/tcp
  • I made a firewall rule to allow connection on the port I'm trying to connect: gcloud compute firewall-rules create default-allow-websockets --allow tcp:<PORT> --description "Allow websocket traffic"

I could use it like this of course but this IP constantly changes once I stop and restart the instance. Hope you got some tips for me!

1

There are 1 best solutions below

1
Michael Ambrose On

Do you know if your hostname is resolving within your app? A lot of times services like this can use split DNS so there's a possibility that your code isn't resolving the same same public IP that you expect.

I'd recommend throwing together a quick test to see what your <DOMAIN> resolves to to ensure it's what you expect.