Library incompatible with Heroku, etc
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?
Awesome idea. Gimme a few hours...
That... is why I love GitHub. :-) Cheers!
Please let me know if I can help...! Great library, btw.
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.
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.
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!
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.
What kinds of errors are you getting? Here are a few things to check...
- Are you creating your own express app and passing it in?
- 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.
- Is your Heroku related setup correct (i.e. Procfile, package.json) ?
- Are your twilio credentials correct and are you using them when creating a new Twilio client?
- What port is your express listening to? You should be doing something like... var port = process.env.PORT || 3000;
- What port is your TwilioClient using? Try specifying port 80 for the 'port' parameter
Thanks for the reply!
- Yes it was something along the lines of: create express, make opts, opts.express = myapp, and then pass opts to TwilioClient
- The url I was using for the voice app was the default one (autoprovision).
- Everything else about Heroku was working except for the routes to TwilioClient
- Yes the same code worked on my machine before moving it to Heroku (and making the address/port changes)
- It was using the method you have
- 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.
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.
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?
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.
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);
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...
"port" : config.port
I can't get my app to work with this patch. What are you using for config.port?
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 + '/';
(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 + '/'
This library is deprecated in favor of the official library - https://github.com/twilio/twilio-node