1

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!')
  })
})
user3802348
  • 2,502
  • 11
  • 36
  • 49
  • No, you can only bind to the one port that Heroku gives you. Maybe you can do something clever like proxy certain requests to your bot service from express. – danneu Oct 14 '16 at 03:11

2 Answers2

2

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

andresk
  • 2,845
  • 1
  • 13
  • 17
0

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

  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](https://meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted](https://stackoverflow.com/help/deleted-answers). – Tyler2P Jun 30 '22 at 15:36