Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set up socket.io chat on heroku?

I have a simple socket.io chat application which I've uploaded to one of the new Heroku 'cedar' stacks.

Now I almost have everything working but I've hit one stumbling block. On my localhost, I open a connection to the socket server from the client with:

// lots of HTML omitted socket = new io.Socket('localhost', {port: 8888}); 

But on Heroku, I obviously must substitute something else in for these values.

I can get the port from the process object on the server like so:

port = process.env.PORT || 8888 

and pass that to the view.

But what do I substitute for 'localhost'?

like image 675
David Tuite Avatar asked Jun 03 '11 06:06

David Tuite


People also ask

Does Socket.IO work on Heroku?

Socket.io + NodeJS doesn't work on Heroku.

Does Socket.IO need a server?

Socket.io, and WebSockets in general, require an http server for the initial upgrade handshake. So even if you don't supply Socket.io with an http server it will create one for you.


1 Answers

The correct way according the article on heroku is:

io.configure(function () {    io.set("transports", ["xhr-polling"]);    io.set("polling duration", 10);  }); socket = new io.Socket(); 

This ensures that io.Socket won't try to use WebSockets.

like image 114
Luke Chadwick Avatar answered Sep 20 '22 02:09

Luke Chadwick