Question

Websocket client can't connect to the server on DigitalOcean app platform

I have a Nuxt 3 application with socket.io running on DigitalOcean app platform where the client can’t connect to the web socket server.

The web socket runs on the same port 3000 as the application, with:

  • nuxt.config.js
import startSocketServer from "./server/sockets"

export default defineNuxtConfig({
  ...
  hooks: {
    listen: (nuxtServer) => startSocketServer(nuxtServer)
  },
  ...
});
  • ./server/sockets/index.js
import {  Server } from "socket.io";

export default (nuxtServer) => {
  const io = new Server(nuxtServer)
  io.on("connection", (socket) => {
    console.log(`connection ${socket.id}`)
  });
};

And on the client I have:

const socketDomain = (isLocal) ? 'localhost:3000' : 'my.domain.com'

const socket = io(socketDomain, {
  transports: ['websocket'],

socket.on("connect", () => {
  console.log('connect')
});

socket.on("connect_error", (err) => {
  console.log(`connect_error due to ${err}`);
});

The application run perfectly locally with nuxi dev or nuxi build && node .output/server/index.mjs but when I deploy on DigitalOcean I get the following:

WebSocket connection to 'wss://my.domain.com/socket.io/?EIO=4&transport=websocket' failed:
doOpen @ entry.3f5d167e.js:21
open @ entry.3f5d167e.js:20
open @ entry.3f5d167e.js:21
$i @ entry.3f5d167e.js:21
A1.exports @ entry.3f5d167e.js:21
open @ entry.3f5d167e.js:21
(anonymous) @ entry.3f5d167e.js:21
invoice.22658294.js:1 connect_error due to Error: websocket error

I also tried with the web socket on a different port, but from my understanding it is not supported on DO.

I also tried with transports: ['polling', 'websocket'] but I got xhr poll error.

What could be the issue?


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

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello! I would like to ask if you have managed to figure out how to fix your problem with Socket.IO? @learntheropes

Bobby Iliev
Site Moderator
Site Moderator badge
May 12, 2023

Hi there,

There are a couple of things I could suggest here:

  • Verify your Websocket Scheme:

    The code for this app included a conditional statement that checked whether the client was connecting over HTTPS. If it was, the server would attempt to use the wss protocol to make a connection. Otherwise, it would attempt to use the ws protocol.

    const protocol = window.location.protocol.includes('https') ? 'wss': 'ws'
    const ws = new WebSocket(`${protocol}://${location.host}`);
    

    When you ran this app locally, the app was not using SSL, and therefore used the ws protocol. On App Platform, apps will always run over HTTPS with SSL. Please verify that the app is able to use wss when deploying a new websocket app to app platform.

  • Externally Expose Your Service and Check that the Port is Correct

    Please make sure that your service is exposed externally. If your code only exposes localhost or if you are using an App Platform worker instead of a service, you will not be able to reach your application externally. In this tutorial, the app is exposed on port 8080. If you’re having trouble accessing the app, ensure that the app’s HTTP port is set to 8080 in the App’s spec.

In case that you are still seeing the problem, feel free to share a link to your GitHub project here so I could try deploying it for further investigation.

Best,

Bobby

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