node.js and socket.io with SSL - javascript

I have a server with fully working wildcard SSL, but socket.io give me error:
Failed to load resource: net::ERR_CONNECTION_CLOSED
On the client side im connecting to the socket like that:
var socket = io.connect('http://pricer.somedomain.com',{secure: true, port:5005});
Server Side:
var io = require('socket.io').listen(5005);
What am i doing wrong?

Your client need to connect through https://, not http://
var socket = io.connect('https://pricer.somedomain.com',{secure: true, port:5005});
I had the same problem yesterday, check on my topic.

Related

Failed to load resource: the server responded with a status of 404 (Not Found) Node.js socket.io

I'm trying to create a server using Node.js and socket.io and it starts perfectly. However, when I'm trying to visit the site of my server through the browser, he writes "Cannot Get /" and in the console gives an error "Failed to Load Resource: The Server Respondd with A Status of 404 (Not Found)". For two days I'm trying to understand where the problem and I will be very grateful to your help. Here is my server code:
const express = require("express");
var http = require("http");
const app = express();
const port = process.env.PORT || 5000;
var server = http.createServer(app);
var io = require("socket.io").listen(server);
//middlewre
app.use(express.json());
io.on("connection", (socket) => {
console.log("connected");
console.log(socket.id, "has joined");
socket.on("/test", (msg) => {
console.log(msg);
})
});
server.listen(port, "0.0.0.0", () => {
console.log("server started");
});
Your server is currently not set up to handle any other http traffic than Websocket connection upgrade requests, which you can not make by entering a url in your browser. Also, the browser is not equipped to negotiate this connection upgrade or to keep the Websocket connection working once established. For this you need some javascript code to run on the client side.
The socket.io library provides everything you need, look at a minimally working example at the link provided below. You should basically just set up your server to serve an html document which provides a context from which the whole websocket connection upgrade can be managed - and the connection maintained - by the socket.io library.
https://socket.io/docs/v2#Minimal-working-example

Socket io Client connection on http Only

My socket io server does not have ssl certificate, so it only works in htpp, on
the other hand I have my web page which has ssl certificate,
So when I'm connecting to my socket web server (in http) on this page (in https), it's changes automatically in https so necessarily I get an error
The connection :
//Here I'm connecting in http
const socket = io("http://XXX.XX.XXX.XX:XXXX",{secure:false})
here you have the pictures which illustrates the problem, i have write "http: // xxxxxxxx" and in the console i got the error:
//But in the error we see that change and it's try to connect with https
"GET https://XXX.XX.XXX.XX:XXXX/socket.io/?EIO=3&transport=polling&t=NFiqorQ
net :: ERR_SSL_PROTOCOL_ERROR"
PS: when i try to connect to the websocket with wamp (so in http) it's work...
if anyone knows how to force the connection to http, please help me
Client side error
Client side conection
im sure it has something to do with your server. i only work with nginx so i can just talk about that but im sure its something similar in apache. I create virtual server blocks in nginx one is for the fileserver where ssl and certificate is enabled. after that i set up a block for the socket and set the server just to listen to http.
upstream socket {
server 127.0.0.1:2095;
server 127.0.0.1:80 backup;
keepalive 15;
}
server {
listen 80;
listen [::]:80;
root /var/www/socket/;
..... and so on
i work with a subdomain always to connect the socket like socket.domain.com but its not needed.
the same but with ssl on and linking certificate i do for the fileserver and this way it works pretty well.
var HOST = '144.125.....';
const socket = io(HOST,{secure:true})

connecting to socket.IO server through apache webproxy

I have 2 node.js apps on my vps. One on port 3001 and one on port 5001(ssl) and 5000(http).
I use apache proxy to route traffic from the homepage to the node.js Servers. it also allows me to easily install a LE certificate.
This is my apache setup
ProxyPass /herstelling http://localhost:5000/
ProxyPassReverse /herstelling http://localhost:5000/
ProxyPass /kaaswijn http://localhost:3001/
ProxyPassReverse /kaaswijn http://localhost:3001/
The site loads, but I'm unable to get my socket.io to connect.
This is the code I'm using
In order to get https working i set up a domain cdn.quintenverhelst.be to get a certificate.
This domain just points directly to the server IP
On the nodeJS Server:
var app = require('express')();
var http = require('http').Server(app);
var port = 5000;
var express = require('express');
var io = require('socket.io')(http);
https.createServer({
//server keys
}, app).listen(5001);
Client side connecting code:
<script src="https://cdn.quintenverhelst.be:5001/socket.io/socket.io.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script> var socket = io.connect("https://cdn.quintenverhelst.be:5001");</script>
<script src="https://cdn.jsdelivr.net/gh/verhelstq/maintenanceReport/engine.js"></script>
When I open this it gives me the error:
socket.io.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
The URL does not resolve to the socket.io.js file. Looks like its not even loading socket IO script into the page:
<script src="https://cdn.quintenverhelst.be:5001/socket.io/socket.io.js"></script>
Try removing the port number, the JS file resolved when I tested on my end.
<script src="https://cdn.quintenverhelst.be/socket.io/socket.io.js"></script>
That should get you at least past this first error.

Standalone socket.io and Websocket

I need to build a socket.io server that will intercept incoming connections from an app which is not stored in the same directory as the server.
The client side app does not contain node.js, thus I'm trying to use a websocket :
Telnet.Socket = new WebSocket('ws://127.0.0.1:3000');
My node.js server does not need a http server but must be a standalone socket.io app. Thus, I've tried the following code :
var io = require('socket.io')();
io.on('connection', function(socket){
console.log('connexion entrante');
});
io.listen(3000);
Unfortunately, the server part does not seem to get the Websocket connection request. My firefox says :
Firefox cannot establish a connection with the server at adress ws://127.0.0.1:3000/.
What am I missing ?
Thx in advance !
socket.io need client use socket.io to connect because it use many kind of connection. For connect websocket only you can use ws node module

socket.io ssl connection in node.js with express framework

I am quite confused about setting up a secure ssl connection on node.js and the bindings created with the socket.io.
It is giving big headaches, as it is constantly refusing to work, and the result in browsers tested are:
GET https://webrtc.romlex.info/socket.io/1/?t=1405428623632 404 (Not Found) socket error
My serverside code:
var options = {
key: fs.readFileSync(__dirname + '/public/video-phone/key.pem'),
cert: fs.readFileSync(__dirname + '/public/video-phone/cert.pem')
};
actually i am only initializing express
var app = express();
should i create server when initializing express, and pass options?
var app = express.createServer(options);
actually i am creating the https server with the options listening on secured port
var server = require('https').createServer(options,app).listen(443);
which way should i initialize socket.io connection?
Actually i am listening on server with options
var io = require('socket.io').listen(server,options);
Should i listen on server only?
var io = require('socket.io').listen(server);
Or should i listen on the secure port and options?
var io = require('socket.io').listen(443,options);
Quite a bit confused, please help!
i am using xhr-polling over websockets
io.set('transports', [
//'websocket',
'xhr-polling',
'jsonp-polling'
]);
the serverside code:
serverside socket.io with ssl
clientside code:
should i add secure param or not?
io.connect("https://webrtc.romlex.info", {secure: true});
Sorry for the questions, i'm quite a newbie on node.js, especially, when ssl connection comes to scene.
Thanks for your suggestions!

Categories

Resources