Question

How to set up socket.io on App Platform

I have a Node Js app with a React Frontend deployed as an App. Unfortunately Im having a hard time making socket.io work outside of localhost… Im using ${APP_URL}/node as a route for backend and it is running on port 80.

const app = express();
const server = http.createServer(app); 
export const io = new Server(server, {
  cors: {
    origin: allowedOrigins,
    methods: ['GET', 'POST'],
  },
});
// Listen for when the client connects via socket.io-client
io.on('connection', socket);
const connectServer = async () => {
  app.set("view engine", "ejs");
  app.use(cookieParser());
  app.use(credentials);
  app.use(cors(corsOptions));
  app.use( express.urlencoded({limit: '50mb', extended: true }));
  app.use((req, res, next) => {
    if (req.originalUrl === '/stripeHook') {
      next();
    } else {
      express.json({ limit: '50mb' })(req, res, next);
    }
  });
  app.use(express.static('static'))

  const PORT = process.env.PORT || 80;
  server.listen(PORT, () =>  console.log(`Server is running on port ${PORT}.`));
}; 
connectServer();

And on client side Im using a hook

  const BASE_URL = process.env.REACT_APP_SOCKET_URL;
  socket = io(BASE_URL);
  if(userId)socket.emit("live", userId)

Im getting POLLING 200 response in my network tab but the response is ‘You need to enable javascript’.

Can someone help me out! Appreciate any help

Show comments

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

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

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

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
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
Get started for free

Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

New accounts only. By submitting your email you agree to our Privacy Policy.

© 2023 DigitalOcean, LLC.