Search code examples
node.jsherokuwebsocketdeploymentuwebsockets

Heroku Node.js and uWebsockets.js R10 Error, failed to bind port


I have created a minimal app for testing deploying a node.js uWebsockets.js server on heroku with a dockerfile. Heroku logs shows that it fails with R10 (boot timeout) error, failed to bind to $PORT, although the logs before it show that it's listening at that port.

server.js

const uWS = require('uWebSockets.js');
const port = process.env.PORT || 9001;
const host = '0.0.0.0'

const app = uWS.App()
    .ws('/*', {
        maxPayloadLength: 16 * 1024 * 1024,
        idleTimeout: 160,
    })
    .listen(port, host, (token) => {
        if (token) {
            console.log('Listening to port ' + port);
        } else {
            console.log('Failed to listen to port ' + port);
        }
    });

Dockerfile

# syntax=docker/dockerfile:1

FROM node:17.0.1
ENV NODE_ENV=production
WORKDIR /app
COPY ["package.json", "package-lock.json*", "./"]
RUN npm install --production
COPY . .
EXPOSE $PORT
CMD [ "npm", "start" ]

Heroku logs

2021-12-02T15:34:50.509492+00:00 app[web.1]: > dummyserver@1.0.0 start
2021-12-02T15:34:50.509492+00:00 app[web.1]: > node server.js
2021-12-02T15:34:50.509492+00:00 app[web.1]: 
2021-12-02T15:34:50.586940+00:00 app[web.1]: Listening to port 39686
2021-12-02T15:35:49.393274+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2021-12-02T15:35:49.424793+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-12-02T15:35:49.596430+00:00 heroku[web.1]: Process exited with status 0

Things I have tried:

  1. used process.env.PORT
  2. used host as 0.0.0.0
  3. Directly pushing to heroku rather than a dockerfile

Solution

  • I did quite a few changes but i think that it was a docker problem and changing my dockerfile to the below snippet made it work.

    # syntax=docker/dockerfile:1
    
    FROM node:17.0.1
    WORKDIR /app
    COPY "package.json" .
    RUN npm install
    COPY . .
    EXPOSE $PORT
    CMD [ "npm", "start" ]
    

    Also made sure to use wss://app-name.herokuapp.com and not ws:// on the frontend to connect.