Question

How to set up Socket.IO using App Platform?

Hello everybody, I hope you are having a great day.

Intro

I have been trying to establish a websocket connection with my App Platform’s server component for a while and still no success. Surprisingly for me everything is working fine on my local machine.

Trouble description

After emitting a Socket.IO event from the Client I get only the following inside the networks’s request Preview (I don’t know how to attach the pictures in here) (DevTools screen, prnt.sc/oqAH4cvjsSxq):

You need to enable JavaScript to run this app.

Details:

I am using the node:cluster for my server. This code was doublechecked using the official Socket.IO docs for node:cluster.

if (cluster.isPrimary) {
  console.log("Primary cluster is running...");

  const httpServer = http.createServer(app);

  // setup sticky sessions
  setupMaster(httpServer, {
    loadBalancingMethod: "least-connection",
  });

  // setup connections between the workers
  setupPrimary();

  const primaryClusterPort = 3265;
  httpServer.listen(primaryClusterPort, () => {
    // Listen on the HTTP server in the worker process
    console.log(`Primary cluster running on port ${primaryClusterPort}...`);
  });

  const workerCount = 2;
  for (let i = 0; i < workerCount; i++) {
    cluster.fork();
  }

  cluster.on("online", function (worker) {
    console.log("Worker " + worker.process.pid + " is online");
  });

  cluster.on("exit", function (worker, code, signal) {
    console.log(
      "Worker " +
        worker.process.pid +
        " died with code: " +
        code +
        ", and signal: " +
        signal
    );
    console.log("Starting a new worker");
    cluster.fork();
  });
} else {
  const httpServer = http.createServer(app); // Create the HTTP server in the worker process

  const io = new Server(httpServer, {
    cors: {
      origin: "*", // Allow all origins
      methods: ["GET", "POST"], // Allow GET and POST methods
      credentials: true, // Allow credentials
    },
  });

  io.adapter(createAdapter());

  // setup connection with the primary process
  setupWorker(io);

  io.on("connection", (socket) => {
    socket.on("exportExcel", (data) => {
      console.log("Socket on `exportExcel`: ", data);

      // for test
      socket.emit("exportExcel", { path: "export905.xlsx" });
    });
  });

  httpServer.listen(PORT, () => {
    // Listen on the HTTP server in the worker process
    console.log(`App listening on port ${PORT}...`);
  });
}

I am using Node v18.12.1, Socket.IO v4.1.3 on the server, Socket.IO v4.6.1 on the client.

I hope anybody can help me and I am ready to provide any additional information if needed. Thank you in advance.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel