-1

My nodejs app is crashing in Heroku. I have two server running in two different ports. One for express routes and another for socket.io. Apps builds fine and deployed in Heroku. It starts, DB connection is OK, then a server daemon also works fine for some period. Then it change state to crashed. No error log after the crash and no reason for crash. Here is log from Heroku,

2020-04-22T14:52:19.980682+00:00 app[web.1]: > iLearn@1.0.0 start /app
2020-04-22T14:52:19.980683+00:00 app[web.1]: > PORT=5000 node server.js
2020-04-22T14:52:19.980683+00:00 app[web.1]: 
2020-04-22T14:52:20.741515+00:00 app[web.1]: 
2020-04-22T14:52:20.782048+00:00 app[web.1]: API server started on: 5000
2020-04-22T14:52:21.087402+00:00 app[web.1]: Messaging Manager started..
2020-04-22T14:52:21.087501+00:00 app[web.1]: MessagingManager::startDaemon
2020-04-22T14:52:21.089358+00:00 app[web.1]: Messaging server running on port:5001
2020-04-22T14:52:21.130079+00:00 app[web.1]: DB Connected
2020-04-22T14:52:21.132905+00:00 app[web.1]: {"timestamp":"2020-04-22T14:52:21.131Z","message":"DB Connected","level":"info"}
2020-04-22T14:52:26.097164+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:31.100561+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:36.107406+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:41.112479+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:46.116481+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:51.121785+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:52:56.127394+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:01.136499+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:06.136680+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:11.144029+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:16.145616+00:00 app[web.1]: MessagingManager::daemon
2020-04-22T14:53:16.792389+00:00 heroku[web.1]: State changed from starting to crashed

Here is my server.js file,

const express = require('express')
const MessagingManager = require("./util/messagingManager")

app = express()
bodyParser = require('body-parser');
require('dotenv').config();
port = process.env.PORT || 5000;

app.listen(port);
console.log('API server started on: ' + port);
app.use(express.static('public'))
//app.use(formidable());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set('view engine', 'pug');
app.set('views', './views')
var routes = require('./routes'); //importing route
app.use('/', routes); //register the route


const messagingApp = express();
const messagingServer = require("http").createServer(messagingApp);
const io = require("socket.io").listen(messagingServer);
let messagingManager = new MessagingManager()
messagingManager.startDaemon()

port = process.env.MESSAGING_PORT || 5001;


io.on("connection", socket => {
    console.log("a user connected");
    let id = socket.handshake.query.id
    console.log(socket.id)
    messagingManager.addConnection(id, socket)

    socket.on("message", msg => {
      if (msg.type == "MESSAGE_READ_STATUS")
       messagingManager.setMessageStatus(msg)
      else
        messagingManager.sendMessage(msg)
    });

    socket.on("disconnect", () => {
      //io.emit("chat message", msg);

    });

    socket.on("endsession", msg => {
      //io.emit("chat message", msg);
      console.log("session ended." + id)
      messagingManager.removeConnection(msg.id)
      socket.disconnect()
    });

    // not used
    socket.on("session", msg => {
      //io.emit("chat message", msg);
      console.log("session established")
      messagingManager.addConnection(msg.id, socket)

    });
});

messagingServer.listen(port, () => console.log("Messaging server running on port:" + port));



muhammad800804
  • 105
  • 1
  • 8
  • Something seems not right with the PORT (`API server started on: 5000`), Heroku should assign you a dynamic port (which is typically not the default one). Do you have .env file which defines the PORT env variable? – Beppe C Apr 22 '20 at 15:48
  • Yes, I have .env file. Port is defined there(PORT=5000). Is there any issue with opening two ports? – muhammad800804 Apr 22 '20 at 15:51
  • I think you might have 2 problems: 1:Heroku allows one port per app (you will need to deploy 2 components) 2:looks like PORT=5000 overrides the Heroku PORT variable which is the one to use – Beppe C Apr 22 '20 at 15:53
  • 1. You mean I need to deploy two app for two ports? 2. I got it. – muhammad800804 Apr 22 '20 at 15:59
  • I think so, as each app is supplied with a $PORT env variable, not 2 – Beppe C Apr 22 '20 at 18:20
  • 1
    Does this answer your question? [Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch)](https://stackoverflow.com/questions/15693192/heroku-node-js-error-web-process-failed-to-bind-to-port-within-60-seconds-of) – ChrisGPT was on strike Apr 23 '20 at 02:00
  • @Chris yes. it partially answers the question. – muhammad800804 Apr 23 '20 at 04:54
  • @chris scenario is different. Here the question is about application crashed without any log. Port binding issue is not clear in log. Also, two port issue is not answered here, Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch) – muhammad800804 Apr 23 '20 at 07:13

1 Answers1

0

It's hard to actually to know why the application crashed as log just says, application crashed without any message. Port binding error was not thrown and app listen callback function is called. Actual issue seems to be port. I have set port in both .env and package.json file. It overrides heroku dynamic port. Thanks @Beppe C to pointing out this. I deleted port variable from both .env and package.json and redeploy the app. Now it is working fine. Another issue is heroku app does not allow two port in the same application. So, I have to create two app. One for express HTTP route and another for socket.io. Hope it helps someone.

muhammad800804
  • 105
  • 1
  • 8