javascript - How do I get my node/socket.io app to use the correct port when deployed to heroku? -


i followed online chat tutorial socket.io make chat application. in app.js file (i used express generator) have...

var port = normalizeport(process.env.port || '3000'); app.set('port', port); 

this means need browse (and socket server running from)...

http://localhost:3000

on page have lines...

  var socket = io.connect('http://localhost:3000');   socket.on('news', function (data) {     console.log(data);     socket.emit('my other event', { my: 'data' });   }); 

when run application locally socket application works. deploy used heroku/node buildpack , when app deployed running on port 80, example on app.heroku.com. have 2 questions...

1) how come app on different port when deployed? (where did happen?) 2) how set page @ correct url , port?

var port = normalizeport(process.env.port || '3000'); app.set('port', port); 

this fine. keep in mind nothing, it's arbitrarily set value in app. in order listen on port, must later tell server listen, app.listen(app.get('port')).

var socket = io.connect('http://localhost:3000'); 

this want change, to:

var socket = io.connect(); 

when you're hosting on heroku, aren't connecting "localhost:3000" (but rather yourapp.herokuapp.com:80, or customdomain.com:80). so, specifying localhost:3000, you're dooming socket.io not able find own server.


Comments