node-twilio icon indicating copy to clipboard operation
node-twilio copied to clipboard

Library incompatible with Heroku, etc

Open clintandrewhall opened this issue 15 years ago • 17 comments

This library is incompatible with hosting platforms like Heroku and Joyent, which provide you with a specific port in which to listen. The AutoUri lib makes it necessary to have an additional port open for Twilio requests. Apps therefore crash on startup.

Can the library accept an existing express instance, and add endpoints to it?

clintandrewhall avatar Jul 06 '11 06:07 clintandrewhall

Awesome idea. Gimme a few hours...

sjwalter avatar Jul 06 '11 06:07 sjwalter

That... is why I love GitHub. :-) Cheers!

Please let me know if I can help...! Great library, btw.

clintandrewhall avatar Jul 06 '11 06:07 clintandrewhall

If you could test when I have a revision done, that'd help a whole lot! I don't really have an awesome testharness app for this, and it'd save me some time getting one together.

sjwalter avatar Jul 06 '11 06:07 sjwalter

Latest push contains the change.

twilio/client now accepts an option called express. You can pass in your already-extant express app as that param, and that'll re-use your express app instead of making a new one. You must still pass in your hostanme (for generating the urls to tell Twilio about), and further, you should make sure you know about the basePath variable (the root that autoprovisioned URIs are generated starting from), because they will pollute your express app's routed URIs, and if you try to double them up (really unlikely, unless your app happens to also use /autoprivision/0 or whatever), bad things will happen.

Change committed to master. Let me know if this is acceptable.

sjwalter avatar Jul 06 '11 06:07 sjwalter

Did some testing and tinkering... just need to change one thing. You don't want to start the listen if the user is managing the express instance: they'll get an exception.

if(opts.express) {
  self.express = opts.express;
}
else {
    self.express = express.createServer(
        express.logger(),
        express.bodyParser()
    );
    self.express.listen(self.port);
}

Other than that, this works on heroku!

clintandrewhall avatar Jul 06 '11 19:07 clintandrewhall

This probably isn't the spot for this, but how did you get this working on Heroku? Depending on what method I use (GET or POST) I have different errors on just the initial request from Twilio.

jwilson avatar Aug 04 '11 13:08 jwilson

What kinds of errors are you getting? Here are a few things to check...

  1. Are you creating your own express app and passing it in?
  2. What's the url you're using for your voice url in Twilio? It should be something like http://****/autoprovision/0 unless you've changed the default.
  3. Is your Heroku related setup correct (i.e. Procfile, package.json) ?
  4. Are your twilio credentials correct and are you using them when creating a new Twilio client?
  5. What port is your express listening to? You should be doing something like... var port = process.env.PORT || 3000;
  6. What port is your TwilioClient using? Try specifying port 80 for the 'port' parameter

Pablo1 avatar Aug 05 '11 06:08 Pablo1

Thanks for the reply!

  1. Yes it was something along the lines of: create express, make opts, opts.express = myapp, and then pass opts to TwilioClient
  2. The url I was using for the voice app was the default one (autoprovision).
  3. Everything else about Heroku was working except for the routes to TwilioClient
  4. Yes the same code worked on my machine before moving it to Heroku (and making the address/port changes)
  5. It was using the method you have
  6. The issue with supplying 80 as the port is that you need to be root to listen in on 80. Since it seems that if you just use the Heroku port they give you the callback to Twilio includes that port which then modifies your VoiceUrl to include that new port. That port is only accessible once you are behind whatever proxy Heroku has setup. I patched AutoUri (I think) to substitued 80 in instead of the Heroku port, but that didn't seem to accomplish much.

jwilson avatar Aug 05 '11 13:08 jwilson

In order for this to work on Heroku or similar, the change I outlined above:

if(opts.express) {
  self.express = opts.express;
}
else {
    self.express = express.createServer(
        express.logger(),
        express.bodyParser()
    );
    self.express.listen(self.port);
}

NEEDS to be implemented in the library. Heroku starts the server... attempting to start it again will cause an error.

I can clone and create a merge request, if you like.

clintandrewhall avatar Aug 05 '11 15:08 clintandrewhall

jwilson... if you're using npm, make the change I have above to line 33 of auto-uri.js and see if that fixes your problems?

clintandrewhall avatar Aug 05 '11 15:08 clintandrewhall

That was the first change that I did actually. To me it just seems like that once this lib was used as an endpoint with express (I think thats what happens) it stopped serving the node-twilio routes (autoprovision). I haven't yet tried to pass express to node-twilio on amazon (moved my app there for the time being) but I can check it out tonight.

jwilson avatar Aug 05 '11 15:08 jwilson

Well, I made that change as was able to deploy to Heroku with no problem:

app = express.createServer();
app.use(express.bodyParser());
app.use(express.cookieParser());

var TwilioClient = require('./lib/node-twilio').Client,
  Twiml = require('./lib/node-twilio').Twiml,
  client = new TwilioClient(config.twilio.sid, config.twilio.token, config.host, {
    "express" : app,
    "port" : config.port
  });

app.get("/" + config.client.name, function(req, res) {
  var out = "hello", phone = client.getPhoneNumber('+1' + config.twilio.number);
  phone.setup(function() {
    phone.sendSms("+1" + config.client.number, out, {}, function(text) {
      logger.debug("Text object: " + sys.inspect(text))
    });
  });
});

app.post("/incoming/sms", function(req, res) {
  logger.debug("Received an SMS.");
  logger.trace(sys.inspect(req.body));
  res.send("<Response><Sms>Thanks!</Sms></Response>");
});

app.listen(process.env.PORT || config.port);

clintandrewhall avatar Aug 05 '11 15:08 clintandrewhall

Hmm..well I have no idea why mine wasn't working. I will attempt to reproduce the issue this weekend. It just seemed weird that all I had to do was change the port and hostname and it worked on amazon. Anyways, will report back with what I find.

Another edit. How did you configure it so that the callback didn't send the Heroku supplied port back to Twilio? If I used (process.env.PORT || 3000) then when the callback url was create by node-twilio it would have the Heroku port. Twilio would then update my VoiceURL to use the new port...

jwilson avatar Aug 05 '11 16:08 jwilson

"port" : config.port

I can't get my app to work with this patch. What are you using for config.port?

sprice avatar Nov 13 '11 00:11 sprice

I had to make one more change to get this to work on Heroku for text messages -

removing self.port from this line: self.baseUri = 'http://' + self.hostname + '/' + self.basePath + '/';

lionstone avatar Dec 28 '11 03:12 lionstone

(Coffeescript ahead. Pretend it's pseudo-code if you like.)

This'll fix the problem within your app. Send the port like so: (assuming you've defined PORT and the TWILIO constants)

client = new Client TWILIO.ACCOUNT_SID, TWILIO.AUTH_TOKEN, TWILIO.APP_URL,
    port: process.env.PORT || PORT

Then follow that with an edit to the global autoUri instance:

autoUri = global.autoUri
autoUri.baseUri = 'http://' + autoUri.hostname + '/' + autoUri.basePath + '/'

jmhnilbog avatar Mar 19 '12 22:03 jmhnilbog

This library is deprecated in favor of the official library - https://github.com/twilio/twilio-node

ukd1 avatar Aug 13 '17 05:08 ukd1