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.
Related
While working with localhost I used the following code to implement socket functionality-
Server Side
var express = require('express');
var app = express();
const io = require('socket.io')(5000);
Client Side
<script src="http://localhost:5000/socket.io/socket.io.js"></script>
<script>const socket = io.connect('http://localhost:5000');</script>
The above code worked fine for me locally. Earlier tried use the code given in the official documentation, but it did not work. I got a 404 error when trying to load socket.io.js file and it said 'io is not defined' on the client side. That code is given below-
Server Side
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
Client Side
<script src="/socket.io/socket.io.js"></script>
<script>var socket = io.connect('http://localhost');</script>
Now I need to deploy the app to Heroku and don't know how to do the socket functionality. I tried both the above ways but none worked in Heroku. I am getting '404 error' and 'io is not defined'. The server-side code is written in ./app.js and client-side code written in ./public/index.html.
How do I get this socket functionality to work in Heroku and also why is the official code not even working locally?
Trying to get the same thing working, don't have all the answers but on heroku you need to grab the port from the environment variable.
// Needed for Heroku serving
if (process.env.PORT > 0)
port = process.env.PORT;
else
port = 5000;
// create the express server (app) and the socket io server (io)
const app = express();
const server = app.listen(port, () => {
console.log("Listening on port: " + port);
});
const io = require('socket.io')(server);
Client Side
const socket = socketIOClient('APPNAME.herokuapp.com:80');
socket.emit("MsgType", "Payload")
I'm pretty new to NodeJS and trying to get Socket.IO running with an Express application. I've tried to stick to the docs and tutorials as close as possible, but in the end, socket.io.js cannot be found on client side.
The server is being started as follows:
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() { debug('...') } );
Routing/serving pages:
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('index', { title: 'Express' });
});
Including socket.io:
var express = require('express');
var http = require('http').Server(express);
var io = require('socket.io')(http);
And in the HTML file, for clients:
<script src="/socket.io/socket.io.js" type="text/javascript">
But when loading the page, this JS-file is never found. I suspect that including socket.io via that previously created http server is somehow wrong... Or am I missing something else?
Update
I just found out that the snipped that includes socket.io is executed before the server is started (first snippet). They're in different files and the server-start one (named www.js) is configured as "start" script in package.json, the other one (websockets.js) required indirectly by that.
If I put
var io = require('socket.io')(server);
at the end of www.js, everything works just fine. But is there a way to do it from websockets.js, which is loaded before, e.g. by a server start callback function?
Try specifying the full path, like this
<script src="http://localhost:8000/socket.io/socket.io.js"></script>
And connect to your server from client side:
var socket = io.connect('http://localhost:8000/');
also check if socket.io module is available in your node_modules directory.
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
i've read the answers here about my problem in node.js, but my problem persists. With socket.io in the server, everything looks ok, but in client side, chrome says:
Failed to load resource: the server responded with a status of 404 (Not Found)
localhost:3382/socket.io/socket.io.js
var express = require("express");
var http = require("http");
var _io = require("socket.io");
var PORT = 3382;
var app = express();
app.listen(PORT, function(){
console.log("Listening from "+PORT);
});
var server = http.createServer(app);
var io = _io.listen(server);
With jade i have:
script(src='/js/jquery.js')
script(src='/js/chat.js')
script(src="socket.io/socket.io.js")
I have the folder js in a folder static, it works ok. I don't know if it's because the versions (node -v show v0.8.16), how do i see versions of express and socket.io?
Read through http://expressjs.com/api.html#app.listen, note how app.listen is defined.
In short: replace app.listen with server.listen.
I successfully configured node with connect and nowjs. Calling localhost:8001/test.html and localhost:8001/chat.html works without any problems. But when calling a PHP-file localhost:8001/test.php my browser wants me to download that file. Obviously connect.static won't interpret the PHP file. Is there any other possibility to get it work for html and php files?
Thanks
var http = require('http');
var connect = require('connect');
var nowjs = require("now");
var app = connect();
app.use(connect.static('/var/www/www.domain.com/htdocs'));
app.use(function(req, res){
res.end();
});
var server = http.createServer(app).listen(8001);
var everyone = nowjs.initialize(server);