Azure + Socket IO produces Error 404 - node.js

NodeJS and socket.io seems to work fine but inspecting the logs I can see an "HTTP Error 404.0 - Not Found" error that I cannot identify why is happening. For example my full url where nodejs is hosted is sample-nodejs.azurewebsites.net
and I get the following error:
Update with more information:
I tried different combinations of setting up the server (using or not express) but I always get the same result.
I initialize my server like this:
var http = require('http');
const PORT = process.env.PORT || 1337;
var server = http.createServer();
var io = require('socket.io')(server);
server.listen(PORT);
io.sockets.on('connection', function(socket)
{
...
});
I used socket.io 1.7.4 through the current latest 2.0.3. and I get the same issue. Debugging the client seems fine but inspecting the Errors from azure "Log stream" I can see randomly (didn't realize a pattern) HTTP 404.0 - Not Found errors and is requesting a url from https://sample-nodejs:80/socket.io/?EIO=3&transport=polling&a..
Isn't the url strange since the full path for the server is sample-nodejs.azurewebsites.net ?

Related

Express server causes error "This site can’t provide a secure connection localhost sent an invalid response"

I'm trying to make a simple hello world kind of server using Express.
Here goes my code:
const express = require("express");
const app = express();
app.get("/",function(request, response){//what to do when someone make get request to the homepage or route
console.log(request);
response.send("hello");
});
app.listen(3000, function(){
console.log("server is listening at port 3000");
});
When I run the program I see this at the command prompt:
server is listening at port 3000
But when I'm accessing it through a browser i.e. https://localhost:3000, I'm getting an error:
This site can’t provide a secure connection localhost sent an invalid
response. Try running Windows Network Diagnostics.
ERR_SSL_PROTOCOL_ERROR
I expect the browser to see hello as per my method, above, response.send("hello")
You need to access it through the HTTP protocol instead. try to connect to http://localhost:3000 instead of https://localhost:3000.
Basically what app.listen does is that it creates an http server and listens on it, so you have to use http protocol to access it and not https.
This is the source code for app listen taken from express:
app.listen

Server responds with 404 when trying to connect Socket.io to an Express app

I'm building a VueJS app using vue-cli's webpack template.
I've split the front and back ends into different Heroku applications and deployed them.
Background:
My client app has the same setup as described here
tl;dr the above Medium article:
We now have a fresh Vue-cli/webpack app, and a server.js file used to create an Express server that serves the built app files.
The problem:
I've been running into issues trying to use socket.io on said server.js file.
Here's how server.js looks like:
var http = require('http'),
path = require('path'),
express = require('express'),
app = express(),
server = http.createServer(app),
socketIO = require('socket.io'),
port = process.env.PORT || 8080,
history = require('connect-history-api-fallback'),
serveStatic = require('serve-static')
app.use(serveStatic(path.join(__dirname, '/dist')))
server.listen(port, () => {
// logs when running node server.js
console.log('listening on port', port)
})
const io = socketIO(server);
io.on('connection', (socket) => {
console.log('Connected!!!');
});
And this is how I call socket.io inside of my .vue component:
const io = require('socket.io-client')
const socket = io('http://localhost:8080')
As soon as this last line is uncommented I receive a friendly Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/socket.io/?EIO=3&transport=polling&t=M9yJX8nsocket.io/?EIO=3&transport=polling&t=M9yHe0F
Additional info:
Not sure if relevant, but still saying - I'm Using "connect-history-api-fallback" to point all non-existent routes to a wildcard 404 .vue component that displays a friendly user message and allows them to go back to existing routes.
Can this be a part of the reason? What I read about my issue is that I probably have trouble making socket.io run on the same server that my app is running in.
I experienced the problem initially when trying to first connect my Vue App with Vue-Socket.io
upon the line
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'));
Where as an URL I used http://localhost:8080
I've spend a good few days on the problem and still have no clarity on where this problem is rooted in. I am really trying to understand and would highly appreciate any form of feedback. I read about people having the same / similar problem, and tried calling io() without my localhost + port url.
First question here, hope it's properly asked.
Okay, guys, I don't know why, but following the code placed in the "Docs" section of Socket IO's website resulted in the same error (Express 3/4 section). I then went on to copy the Chat demo and had success, so I'm now importing Socket.IO in my Index.html file
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
With server.js looking like this:
var path = require('path'),
app = require('express')(),
http = require('http').Server(app),
io = require('socket.io')(http),
port = process.env.PORT || 8080,
history = require('connect-history-api-fallback'),
serveStatic = require('serve-static')
app.use(serveStatic(path.join(__dirname, '/dist')))
http.listen(port, () => {
console.log('listening on port', port)
})
io.on('connection', (socket) => {
console.log('Connected!!!');
});
I'm going to close this now and see how to implement the functionality I'm looking for - I now have a good starting point. Best of luck to anyone struggling with this.
Cheers!

Socket.io, server not responding to connection event

I have a managed server at cloudways. I am trying to use socket.io.
client.php:
var socket = io('http://localhost:3000');
console.log(socket);
server.js:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
io.on('connection', function(socket){
console.log('is connected');
});
When visiting the page, I see two instances of 200 OK in the network section of the console.
The console in the browser also prints the socket variable, which has "connected : true".
However, I DO NOT see "is connected" in the terminal (from server.js), it is just blank.
This site has worked locally, but these problems are appearing when trying to get it to work at Cloudways.
Any ideas?
I have tried switching "localhost" to both the domain name and the IP-address, and I have tried different ports, but 3000 is the only one that will let me start the server.js without error.
EDIT
Cloudways have confirmed that port 3000 should be used.
I have tried changing the url in io() to:
var socket = io('http://my-cloudways-url:3000'); (net::ERR_EMPTY_RESPONSE)
var socket = io('http://www.my-domain.club:3000'); (net::ERR_EMPTY_RESPONSE)
var socket = io('http://ip-adr:3000'); (net::ERR_EMPTY_RESPONSE)
And today I am getting
var socket = io('http://localhost:3000'); (net::ERR_CONNECTION_REFUSED)
I have also tried io.connect(using the same adresses / ports).
Maybe is your php index... Because you are setting the localhost and the port.
I'm not sure about how Cloudway works. But, when we run the server.js, some clouds give some url for access our projects...
In your cloudways try to use the official URL that they given to you access the project:
var socket = io.connect('url.from.cloudway');
For example, I'm using cloud9, and when I execute the server.js, return one url like:
Your code is running: https://demo-some-id.c9.io
And when I'm using socket.io, I need to set in my index the URL parameter inside the IO.
var socket = io('https://demo-some-id.c9.io');
You need to add the client-side socket.io.js version and add the script tag with the path, like:
<script src="js/socket.io.js"></script>
Obs.: Some clouds, you need to set the url in your <script> tag
<script src="https://demo-some-id.c9.io/socket.io/socket.io.js"></script>

Heroku socket.io sample error in Express 4.0

So I'm trying to use the sample heroku application: https://github.com/lstoll/socket-io-chat-heroku as a template to build my own socket.io application, but I'm running on Express 4.0, Node 0.10.x, and Socket.io 0.9.16.
Now the problem is that when I run my server, everything is fine, but when I run my client, I get the following error:
Uncaught ReferenceError: require is not defined socket.io.js:12
Uncaught ReferenceError: io is not defined chat2:2
My relevant server code is as follows:
var app = express();
var http = require('http');
var server = http.createServer(app);
var sio = require('socket.io');
var port = 3000 || process.env.PORT;
server.listen(port);
var io = sio.listen(server);
io.sockets.on('connection'), function(socket) {
...
});
On my client side, I have the following:
I've tried both (this is in jade, by the way):
script(src='/socket.io/socket.io.js') OR script(src='http://localhost:3000/socket.io/socket.io.js')
var socket = io.connect() OR var socket = io.connect('http://localhost:3000')
Neither of these options have worked, always resulting in an error on the client side.
Is there something special to do for Express 4.0? I've asked a very similar question here: Node.js /socket.io/socket.io.js not found express 4.0 but this is another attempt at getting chat to work with a different template.
Update and Edit: after some work, I was able to deploy a heroku app using express 4.0 and socket.io, at: http://salty-escarpment-7398.herokuapp.com/chat.
The problem now is to integrate it back into my current app, and after much work, I now am getting an error:
22:19:56 web.1 | GET /socket.io/?EIO=2&transport=polling 404 26ms - 1.67kb
22:19:59 web.1 | GET /socket.io/?EIO=2&transport=polling 404 25ms - 1.67kb
I have:
io.set('transports', ['xhr-polling']);
io.set('polling duration', 10);
To set it to xhr-polling, and my server code is pretty much identical to what was above. The page loads, though, and it's only when trying to send a chat that nothing happens and the 404 starts appearing.
Thanks
By default Socket.IO 1.0.4 allows polling and websocket transports. You removed polling transport ! return the polling transport back:
io.set('transports', ['websocket',
'flashsocket',
'htmlfile',
'xhr-polling',
'jsonp-polling',
'polling']);

Setting up Cloud9 SSL App with Node JS

I've been playing around with the Cloud9 IDE and am having a great time with it. However, I'm trying to setup a simple https server with node js and I can't seem to get it to work. When I run the page, Cloud9 says 'Running Node Process' but when I visit the url that the server is supposed to respond to: https://workspace.user.c9.io the page says
Service Temporarily Unavailable
The server you are trying to contact is down either because it was stopped or is unable to service your request due to maintenance downtime or capacity problems. Please try again later.
node-web-proxy/0.4 Server at project-livec9f70a01ca28.rhcloud.com Port 8000
I created a test certificate with OPENSSL and am using the following code to set up my server. I can confirm that the OPENSSL certificate was built correctly.
var https = require("https");
var fs = require("fs");
var url = require("url");
var options = {
key: fs.readFileSync('certs/cert.pem'),
cert: fs.readFileSync('certs/cert.pem')
};
// create a server
https.createServer(options, function(req, res) {
console.log("This works!");
res.writeHead(200);
res.end("Hello world from Cloud9! Url:"+req.url);
}).listen(process.env.PORT);
Thank you for your help!
you need .listen(process.env.PORT,process.env.IP);
It should say that when you start a program.

Resources