Report this

What is the reason for this report?

How to set up socket.io on App Platform

Posted on November 4, 2022

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



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.

I’m curious if you ever figured out an answer to this? Looking to do the same.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.