I am trying to “deploy” my program that works with a web socket server. I am using Heroku but encountering the following error:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I have spent several days stuck on this issue and keep going online to see recommendations about using Heroku but the changes dont seem to work. The code works locally if I use localhost instead of 0.0.0.0 and don’t use process.env.PORT. In that case too though, heroku open still gives an error.
Here’ a look at my code:
PROCFILE:
web: node server.js
SERVER.JS
const WebSocket = require("ws");
const PORT = process.env.PORT || 3000;
const server = new WebSocket.Server({port: PORT});
server.on("listening", () => {
console.log(`WebSocket server is listening on port ${PORT}`);
});
server.on("error", (error) => {
console.error("WebSocket server encountered an error:", error);
});
server.on("connection", ws => {
console.log("Client connected!");
INDEX.HTML
const PORT = process.env.PORT || 3000;
const ws = new WebSocket(`ws://0.0.0.0:${PORT}`)
ws.addEventListener("open", () => {
console.log("Connected!")
ws.addEventListener("message", event => {
const newData = JSON.parse(event.data)
The main recommendation I saw online was about Heroku’s dynamic ports and use of a global host but it doesnt seem to help. As an extra fyi, even if I change it to ws://localhost:${PORT} nothing happens.