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.