user3802348
user3802348

Reputation: 2582

Open two different ports in Heroku app

I'm currently using botkit-sms with Node and Express, deployed on Heroku to create a mobile application that can send SMS messages to users who aren't yet signed up. In my server.js file, I open ports for the API and the messaging bot, which works locally but breaks when I deploy to Heroku. I get the error Error: listen EADDRINUSE :::56136. I know this is because both are trying to use process.env.PORT. Is there a way I can fix this?

Relevant code:

import express from 'express';

const app = express();

const TwilioSMSBot = require('botkit-sms')
const controller = TwilioSMSBot({
  account_sid: ACCOUNT_ID,
  auth_token: AUTH_TOKEN,
  twilio_number: TWILIO_NUMBER
})

const port = process.env.PORT || 9090;
app.listen(port);

app.get('/', (req, res) => {
  res.send('hi');
});

let bot = controller.spawn({})

controller.setupWebserver(process.env.PORT ||  3001, function (err, webserver) {
  controller.createWebhookEndpoints(controller.webserver, bot, function () {
    console.log('TwilioSMSBot is online!')
  })
})

Upvotes: 1

Views: 9045

Answers (2)

fabiosalvi
fabiosalvi

Reputation: 1

It IS possible to use different ports on Heroku, as long as the default port (named $PORT) is among those.

See here for an exmple of multiple ports used: https://stackoverflow.com/a/43911373/9646899

Upvotes: 0

andresk
andresk

Reputation: 2845

I don't know how the bot works, but I think that the only possible workaround is to make it use Websockets or Socket.io as described here, as Heroku doesn't allow the use of different ports

Upvotes: 2

Related Questions