Saeed M.
Saeed M.

Reputation: 2361

socket.io doesn't work on heroku

I'm new to socket.io and heroku.I wrote the simple chat system but when I publish that on Heroku it doesn't work. app.js

var port = process.env.PORT || 3000;

var io   = require('socket.io')(port);
// var url  = require('url');


io.on('connection', function(socket,d) {
    socket.on('sendMsg', function(data) {
        console.log('SEND msg');
        console.log(data);
        io.emit('getMsg',data);
    });

    console.log(socket.handshake.query.id);
});

and in client :

 <script src="http://chatserversm.herokuapp.com:3000/socket.io/socket.io.js"></script>
        <script>
            var socket = io('http://chatserversm.herokuapp.com:3000?id=2');
            ....

Where I was wrong ?

Upvotes: 1

Views: 1079

Answers (1)

ldmat
ldmat

Reputation: 1041

Your client attempts to connect to port 3000, but on Heroku process.env.PORT is set to 80.

Heroku only exposes port 80 (and 443 for SSL), so you will have to change your client to connect to that port.

Note that it should work just fine with an HTTP server on the same port.

Edit: here's what you should do.

First, you need your client to connect to the right port:

var socket = io('http://chatserversm.herokuapp.com:80?id=2');

Since port 80 is already implied, you can also write this as:

var socket = io('http://chatserversm.herokuapp.com?id=2');

Another similar issue is that you're trying to get socket.io.js from port 3000, so you also need to change to:

<script src="http://chatserversm.herokuapp.com/socket.io/socket.io.js"></script>

Upvotes: 1

Related Questions