Home > Net >  Socket.io is working on localhost but not working after deploying heroku?
Socket.io is working on localhost but not working after deploying heroku?

Time:12-11

Here is the code of my socket server

const io = require("socket.io")(8080, {
cors: {
  // origin: "http://localhost:3000", 
     origin: "https://mern-bubble.herokuapp.com",
    }, 
});

and herer is my client side code

useEffect(() => {
   // socket.current = io("ws://localhost:8800");
      socket.current = io("ws://mern-bubble.herokuapp.com:8800");

       socket.current.on("getUser", (data) => {
       console.log("data from socket", data);
       dispatch(updateUserInfo(data));
    });
 }, [dispatch]);

It works completely fine when i am working on localhost, But when i deploy my project on heroku, socket.io is not responding anything ?

CodePudding user response:

So I think that port 8800 isn't open on heroku. You should stick to port 3000 and try again. https://devcenter.heroku.com/articles/node-websockets#option-2-socket-io

CodePudding user response:

Heroku's port are dynamically assigned when starting a Dyno. Instead of hard coding the port 8800, use the environment variable $PORT instead.

  • Related