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 17:54

2011-06-25T21:41:31+00:00 heroku[router]: Error H13 (Connection closed without response) -> GET appxxxx.herokuapp.com/socket.io/1/websocket/4fd434d5caad5028b1af690599f4ca8e dyno=web.1 queue= wait= service= status=503 bytes=

Does this maybe mean the heroku router infront of the app is not configured to handle web socket traffic?

[update] It would appear as of 6/22/2011 the answer is yes... heroku does not support socket.io see this post: http://blog.heroku.com/archives/2011/6/22/the_new_heroku_2_node_js_new_http_routing_capabilities/

查看更多
我只想做你的唯一
3楼-- · 2019-03-09 17:57

Wouldn't you just put your actual hostname?

查看更多
Viruses.
4楼-- · 2019-03-09 18:02

This has now changed as of Oct 2013, heroku have added websocket support:

https://devcenter.heroku.com/articles/node-websockets

Use:

heroku labs:enable websockets

To enable websockets and dont forget to remove:

io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 10); 
}); 
查看更多
仙女界的扛把子
5楼-- · 2019-03-09 18:04

I was also having this problem on heroku. I was able to make it work using the hostname "myapp.herokuapp.com" (or simply window.location.hostname, to work both local and in production) and setting the port to 80. I'm using SocketIO 0.6.0.

查看更多
劫难
6楼-- · 2019-03-09 18:11

I was able to get Socket.IO v0.8 to work on Heroku Cedar by doing the following:

Within the Express app (in CoffeeScript in my case):

app = express.createServer();
socket = require("socket.io")

...

io = socket.listen(app);
io.configure () ->
  io.set("transports", ["xhr-polling"])
  io.set("polling duration", 10)

io.sockets.on('connection', (socket) ->
  socket.on('myaction', (data) ->
    ...
    socket.emit('result', {myData: data})

### The port setting is needed by Heroku or your app won't start
port = process.env.PORT || 3000;
app.listen(port);


And within the front-facing Javascript of your application:

var socket = io.connect(window.location.hostname);
function sendSocketRequest() {
  socket.emit('myaction', $("#some_field").val());
}

socket.on('result', function(data) {
  console.log(data);
}

Helpful links:

查看更多
你好瞎i
7楼-- · 2019-03-09 18:11

After trying every combination under the sun I finally just left it blank. Lo and behold that works perfectly. You don't even need the port.

socket = new io.Socket();
查看更多
登录 后发表回答