skip to Main Content

I have a Node.js server using socket.io to connect Android apps and I’ve been hosting it locally by just running it in my IDE and connecting to my local IPv4 address but I want it to work without me having to keep my PC running constantly so I’ve tried using Google Cloud and managed to get it mostly working but the client doesn’t keep the connection and disconnects consistently.

I followed this tutorial up to step 4 after that I ran gcloud app deploy.

My Node.js server is in one file, it has these declarations at the top.

const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

I then have this for the initial client connection.

io.on("connection", (socket) => {
    console.log("User connected.");

Everything inside of this is just listeners for what gets emitted by the client so I don’t think they’re the problem.

And then outside of that I have

server.listen(8080, () => {
    console.log("Server is listening");
});

I don’t know if anything from the package.json file is relevant but I can provide it if need be.

After deploying to Google Cloud using the tutorial there are a few things in the logs that may be the reason behind the problem.

Waiting for network connection open. Subject:"app/invalid" Address:127.0.0.1:8080

Waiting for network connection open. Subject:"app/invalid" Address:127.0.0.1:8081

Wait successful. Subject:"app/invalid" Address:127.0.0.1:8080 Attempts:97 Elapsed:485.916418ms

App is listening on port 8080. We recommend your app listen on the port defined by the PORT environment variable to take advantage of an NGINX layer on port 8080.

These might have simple solutions but I have very little experience with server hosting.

Once my Android client connects to the server, the log outputs "User connected." as it should, then around 10 seconds later it does it again and this repeats. There’s no error I can see between the connections just a few socket.io POST/GET requests.

I tried adding session affinity to the app.yaml but hasn’t solved it either.

This is my app.yaml if any changes need to be made here

runtime: nodejs16
env: standard
instance_class: F1
automatic_scaling:
  min_idle_instances: automatic
  max_idle_instances: automatic
  min_pending_latency: automatic
  max_pending_latency: automatic
network:
  session_affinity: true

Thanks for any help, if I need to provide any other files I can do so.

2

Answers


  1. Messages you have pasted are most likely not indicating a problem. Your application is running in a sandbox environment for which a container instance and a web server must be initialized the first time that your app engine service receives a request. This is also called Loading request and the messages you see indicate the startup of your container instance and your webserver.
    You can take a look at Google’s own documentation regarding handling requests in node.js or follow a quickstart guide.
    If there are no other logs during the disconnection, I would suggest checking the quotas to see if you’re not exceeding any.

    Login or Signup to reply.
  2. Socket communication requires persistent network connections. So, App Engine Flexible provides an option to keep the network connection alive.
    I think you are deploying your app in App Engine Standard , which does not support this option.
    So, you can deploy your app in App Engine Flexible and you need to include the following configuration in your app.yaml

    network:
        session_affinity: true
    

    For refence on session affinity, please refer to this page https://cloud.google.com/appengine/docs/flexible/nodejs/using-websockets-and-session-affinity#session_affinity
    So, your updated app.yaml can look like this

    runtime: nodejs
    env: flex
    network:
      session_affinity: true
    

    Please refer to this page for supported yaml configuration for flexible environment – https://cloud.google.com/appengine/docs/flexible/nodejs/reference/app-yaml

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search