I'm doing this simple test on my nodejs app in Openshift. The code below was running fine and I was able to see index.html, until I added the line var io = require('socket.io').listen(http);. Now I'm getting the 503 - Service Temporarily Unavailable. I have socket.io installed in the app-root/repo/node_modules folder, and I've also included it to dependencies in package.json. What could be wrong?
var express = require('express');
var app = express();
var http = require('http').Server(app);
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var io = require('socket.io').listen(http);
app.get('/', function(req, res) {
res.sendfile('index.html');
});
http.listen(server_port, server_ip_address);
Your code works OK on my machine. Double check the dependencies, and above all the logs:
ssh to your gear
less app-root/logs/nodejs.log
Related
I have code snippet to explain what i am doing and what i want.
var express = require('express');
var http = require('http');
var app = express();
app.use('/', express.static(__dirname + '/static'));
var BinaryServer = require('binaryjs').BinaryServer;
var server = http.createServer(app);
var binaryServer = new BinaryServer({server:server});
var ioServer = http.createServer(app);
var io = require('socket.io').listen(ioServer);
I can run node express and socket.io on same port.
ioServer.listen(8080, function(){
console.log('server running at localhost:8080');
});
Same can be done with node express and binaryServer.
server.listen(8080, function(){
console.log('server running at localhost:8080');
});
But i want to run node express, socket.io and binaryServer on same port express is running (8080 in this case).
Any suggestions ?
You would need to attach both the SocketIO and binaryServer to same http server instance then bring that single instance up.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var binaryServer = new BinaryServer({ server:server, path: '/binary'});
server.listen(8080, function(){
console.log('http/socket/binary server running at localhost:8080');
});
Set the path so binaryServer doesn't conflict with any of your apps. This path is required in the client connections too.
I'm hosting my application on openshift. I am using a custom domain. And socket.io wasn't able to download the client side script so I just used the cdn instead. But now it's not able to connect to a namespace. These are the errors it is giving me on the console log
This is my client side code on the .html page to download the client side script
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
and the .js index page to connect to the index namespace
var socket = io("http://www.loomius.com/index");
Here is my server side code
var express = require('express');
var app = express();
var http = require('http');
var io = require('socket.io')(http);
var mongoose = require('mongoose');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var https = require('https');
// listening on the port
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
});
First instead of
var socket = io("http://www.loomius.com/index");
use this to solve issue with a need of cdn
var socket = io.connect("/");
Then on server use this instead
http = http.createServer( app ).listen( process.env.PORT, process.env.IP || "0.0.0.0", function() { // or define ip and port manually
var io = require( 'socket.io' )( http );
io.on('connection', function( socket ) {
// add event listeners here
}
});
When I am deploying the app in heroku and running, it gives Cannot GET/ in the browser.
I cannot understand where exactly the problem is !
var WebSocketServer = require("ws").Server;
var http = require("http");
var express = require("express");
var app = express();
var port = process.env.PORT || 5000;
app.use(express.static(__dirname + "/"));
var server = http.createServer(app);
server.listen(port);
client.js
// When running in heroku
var host = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(host);
// When running on local machine
var connection = new WebSocket('ws://127.0.0.1:5000');
Local Folder Structure:
.git
node_modules // containing ws and express
client.js
index.html // includes client.js
server.js
package.json
I have developed a very simple appp in node.js its working fine locally using express and socket.io. But when i deploy this on azure website using git it gives me error. I figure out that line that initialize socket.io throws. Following is my server.js file.
//try this
var http = require('http')
var fs = require("fs");
var express = require("express");
var twitter = require('ntwitter');
var app = express();
var port = process.env.PORT || 1337;
app.get("/", function(request, response){
var content = fs.readFileSync("template.html");
// console.log("Contents");
// console.log(content);
response.setHeader("Content-Type", "text/html");
response.send(content);
});
var server = app.listen(port, function() {
//console.log('Listening on port %d', server.address().port);
});
Following line works locally but on azure gives throws error
var io = require('socket.io').listen(server); //working on localhost
multiple folders named "emitter" that contained the indexof module also had a gitignore file that made git ignore the module, no idea why that was even there, but deleting them fixed the problem
used git add -A --force it solved it for azure and heroku after push. socket.io working perfectly.
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.
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.