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:
import startSocketServer from "./server/sockets"
export default defineNuxtConfig({
...
hooks: {
listen: (nuxtServer) => startSocketServer(nuxtServer)
},
...
});
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?
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!
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.
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 thews
protocol.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 usewss
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 to8080
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