Can I set up socket.io chat on heroku?

2019-03-09 17:22发布

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'?

7条回答
走好不送
2楼-- · 2019-03-09 18:12

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.

查看更多
登录 后发表回答