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


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