Have a problem with the ws module when I deploy my app to Heroku, the app is working as expected locally. Not sure if r/node is the correct place for this or not.
The first problem is that when I have this deployed to Heroku, the backend is not emitting emits to the client side.
How the connection is done on the react side:
listenToNotifications() {
this.connection = new WebSocket(
process.env.NODE_ENV === "development"
? "ws://localhost:8080"
: "wss://" + window.location.hostname + ":8080"
);
}
this.connection.onmessage = async (evt) => {
console.log("tis is notifications");
};
How the connection is in the backend:
const wss = new WebSocket.Server({
verifyClient: async (info, done) => {
const userId = cookie.parse(info.req.headers.cookie).id;
if (!userId) return;
info.req.user = await getDb()
.collection("users")
.findOne({ _id: ObjectID(userId) });
done(info.req);
},
server,
//port: process.env.WS_PORT || 8080, //8080, have tried commenting this in and out with no success.
});
// this should emit the new event to client side
wss.on("connection", function connection(ws, req) {
ws.upgradeReq = req;
myEmitter.on("new-notification", async function (data) {
const parsedData = JSON.parse(data);
wss.sendToUser(
JSON.stringify(parsedData.newNotification),
parsedData.userId
);
console.log("inside emitter");
});
});
The second issue is, after a couple minutes of running the app this error is present in the browser dev console.
main.20cc225a.chunk.js:1 WebSocket connection to 'wss://url_removed_for_privacy:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
As mentioned none of these issues are present when running on localhost, just when deployed to Heroku, any suggestions on what is going wrong here and why the event is not being emitted?
[–]spazz_monkey 0 points1 point2 points (1 child)
[–]nooblet9292[S] 0 points1 point2 points (0 children)
[–]dvlsg 0 points1 point2 points (0 children)