1

I am struggling with an Express/Node/PeerJs application that uses socket.io. It works in localhost but not when pushing to heroku or nodejitsu.

Here is what I have in app.js:

var routes = require('./routes');
var express = require('express');
var path = require('path');
var https = require('https');
var fs = require('fs');
var url = require('url');
var app = express(), 
    http = require('http'),
    server = http.createServer(app), 
    io = require('socket.io');

app.engine('.html', require('ejs').__express);
app.set('port', process.env.PORT || 5500);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
...

console.log(process.env.port);

app.configure('development', function(){
    app.use(express.errorHandler());
});

server.listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

var sockets = io.listen(server);

app.get('/', function(req, res){
  res.render('index');
});

//more code

In chatroom.html:

<script type="text/javascript"></script>
<script src="myapp.herokuapp.com:5500/socket.io/socket.io.js"></script>
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>

var socket = io.connect('http://myapp.herokuapp.com:5500/');
var peer = new Peer({key: 'mykey', debug: true});

peer.on('open', function(peer) {
    peer_id = peer;
    console.log('my peer id is ' + peer_id);
    socket.emit('peer', {peer_id: peer_id, chatroom: chatroomString});
    console.log('peer id and chatroom string: ', {peer_id: peer_id, chatroom: chatroomString});
});
//rest of code

I've tried changing the path to /socket.io/socket.io.js, adding http, changing ports, and many other silly things. Yet unfortunately it works in localhost but not when I push to both nodejitsu and heroku.

There are some relatively similar questions(that I could find) but none with sufficient answers. Socket.io Failed to load resource socket.io: Failed to load resource

Would appreciate the help.

SOLUTION!!!

in my app.js file

var app = express(),
    http = require('http'),
    server = http.createServer(app), 
    io = require('socket.io').listen(server);
var sockets = io;

in my chatroom.html file

<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://myapp.herokuapp.com/');

You just take the port numbers out of your html file.

Community
  • 1
  • 1
user3103887
  • 11
  • 1
  • 3
  • You're not using a proper URL for loading `socket.io.js`. If that's not the issue, check your browsers console to see if any errors pop up. – robertklep Dec 15 '13 at 08:25
  • thanks. I've actually tried '/socket.io/socket.io.js' and all variations of "http:myapp.herokuapp.com:5500/socket.io/socket.io.js' and "myapp.herokuapp.com:5500/socket.io/socket.io.js'. None of them work. – user3103887 Dec 15 '13 at 15:06
  • 1
    SOLUTION!!! Take out the port numbers when pushing to heroku! – user3103887 Dec 15 '13 at 16:24

1 Answers1

0

Websockets was not supported earlier in Heroku. Now it is supported. You need to set flag for that.

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

Even after setting the flag, and restarting the app, the sockets are not working, please clarify the exact error that you are getting.

Jinto
  • 847
  • 1
  • 11
  • 27