I'm made a full stack mern application and hosted the nodejs backend in heroku and the frontend is running on netlify. All .env variables are set up both for heroku and netlify, I set up cors() also. The website has chat functionality so I use socket.io for that which that used to work nice in development.
This is link to my heroku server (backend api) https://ural-shop.herokuapp.com/
In messenger.js file (This is where the socket connections happens)
import React, { useEffect, useState, useRef } from "react";
import io from "socket.io-client";
const socketRef = useRef();
useEffect(() => {
socketRef.current = io.connect("https://ural-shop.herokuapp.com/");
},[]);
index.js file (main file of backend)
const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const socket = require("socket.io");
app.use(cors());
const io = socket(server, {
cors: { origin: "https://ural-shop.herokuapp.com/" },
});
server.listen(PORT, () => console.log(`Server on port ${PORT}`));
I only put the neccessary parts of the files.
From google developer console I see this.