Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Is it possible to deploy an express api and socket.io server in the same server?

I'm building an api for an api but want socket.io as well listening on different port, I served it on my local machine and it seems to work fine.As soon as I deploy it it does not work.

Server:

// Import stuff...

const app: express.Express = express();
const httpServer: any = createServer(app);
const io = new Server(httpServer);

const PORT: number = Number(process.env.PORT) || 3000;
const SOCKET_PORT: number = Number(process.env.SOCKET_PORT) || 3001;

dotenv.config();
debug.init();

mongoose.connect(String(process.env.DATABASE_URI));

// Endpoints...

// Socket server:

io.on("connection", (socket: Socket): void => {
  socket.on(
    "server-message",
    async (
      payload: MessageSchemaType,
      key: string,
      responseToken: string
    ): Promise<void> => {
      if (key === process.env.KEY) {
        io.emit(`client-message:room(${payload.room})`, payload);

       }
  );
});

app.listen(PORT, (): void => {
  debug.log(
    `Server Ready and listening on port ${PORT}, press CTRL + C to stop operation.`
  );
});

httpServer.listen(SOCKET_PORT, (): void => {
  debug.log(`Socket listening on port ${SOCKET_PORT}`);
});

Client:

// Import stuff...

// This is not working:
const socket = io("https://myapp.herokuapp.com:3001/")

It seems that the client is not connecting to the server and it's giving me a error saying "connection refused"

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda