0

I am running a node app via pm2 using cluster mode (-i 4) and this same node app uses socket.io and since running in cluster mode, I have been not been able to connect to the socket.

I have followed this tutorial to use @socket.io/pm2 instead of pm2 and my code still behaves the same before and after. I have also tried setting transports to only websocket in my client connection and this does connect, but distributes connections throughout the cluster which is not what is intended.

For some reference, here is the code I am working with:

//Some server code:
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/cluster-adapter";
import { setupWorker } from "@socket.io/sticky";
const setupSocketServer = (httpServer) => {
 const io = new Server(httpServer, { cors: { origin: "*" } });
 io.adapter(createAdapter());
 setupWorker(io);
 io.on("connection", async (socket) => {
 
 }
}
//Some client code
import { io, Socket } from "socket.io-client";
socketRef.current = io("https://www.website.com");
setSocket(socketRef.current);

Additionally, I am using Caddy as a reverse proxy. The existing caddy config worked before I ran as a cluster and also when I run with websocket as the only client transport.

website.com {
 redir https://www.website.com{uri} permanent
}
www.website.com {
 encode zstd gzip
 reverse_proxy /api* localhost:3000
 reverse_proxy /socket.io* localhost:3000
 reverse_proxy /* localhost:8080
}

Any help is greatly appreciated, I cannot seem to figure this out. If more context is needed, don't hesitate to ask!

asked Mar 11, 2025 at 3:57

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.