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"