I made a socket.io http server but now I want it to work with https.
I generated a certificate with letsencrypt and I want to use it for my app.
Server code
const fs = require('fs');
const server = require('https').createServer({
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem')
});
const io = require('socket.io')(server, {});
io.on('connection', socket => { console.log(socket.id); });
server.listen(3000);
I'm using socket.io#^2.3.0 and socket.io-client#^2.4.0 for the client
When i try to connect with the client I see no connection / error on the server.
This is how the client looks
const socket = require("socket.io-client")("https://example.com:3000")
socket.on("connect", () => {
console.log("connected")
}
I've also tried with other URLs but still nothing:
const socket = require("socket.io-client")("http://example.com:3000")
const socket = require("socket.io-client")("https://example.com")
const socket = require("socket.io-client")("wss://example.com:3000")
const socket = require("socket.io-client")("wss://example.com")
Related
My error isindex.js:83 WebSocket connection to 'wss://54.38.211.175:3000/socket.io/?EIO=3&transport=websocket' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID
Server codes
var fs = require('fs');
var options = {
key: fs.readFileSync('privateKey.key'),
cert: fs.readFileSync('certificate.crt')};
var app = require('https').createServer(options);
var io = require('socket.io')(app);
io.on('connection', function(socket) {
socket.emit('on_test', {'x': 1});
});
app.listen(3000);
Client codes
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script>var HOST = "https://54.38.211.175:3000"; // PUT YOUR VPS IP THERE
var SOCKET = null;
if (!SOCKET) {
SOCKET = io.connect(HOST, {rejectUnauthorized: false ,secure: true,transports: ['websocket']});
SOCKET["on"]("connect", function (b) {
console["log"]("baglandim");
});
SOCKET["on"]("connect_error", function (a) {
console.error("eror =" + a);
});
console.log(SOCKET);
}</script>
i create ssl but its doesnt work.my site works with SSL so I'm not connecting to socketio with http
i solve .
the certificate of the website connected to the socketio nodejs must be read.
for plesk
var fs = require("fs");
var options = {
key: fs.readFileSync(
"/usr/local/psa/var/modules/letsencrypt/etc/live/YOURSITE/privkey.pem"
),
cert: fs.readFileSync(
"/usr/local/psa/var/modules/letsencrypt/etc/live/YOURSITE/fullchain.pem"
)
};
var app = require("https").createServer(options);
var io = require("socket.io").listen(app);
app.listen(2083);
//app.listen(PORT);
console.log('acigim');
I am using socket io to sent some notifications Messages to the web ui, but currently I get this error message:
https://12.123.12.12:3000/socket.io/?EIO=3&transport=polling&t=MMfbmct
0 ()
My socket.js file:
var fs = require('fs');
var app = require('https').createServer({
key: fs.readFileSync('/opt/bitnami/apache2/conf/serverKey.pem'),
cert: fs.readFileSync('/opt/bitnami/apache2/conf/serverCrt.pem')
}, handler);
var io = require('socket.io')(app);
var Redis = require('ioredis');
var redis = new Redis();
var Redis = require('ioredis');
function handler(req, res) {
res.writeHead(200);
res.end('');
}
io.on('connection', function(socket) {});
// ...
// run server on port 3000
app.listen(3000, function () {
console.log('Server running!');
});
The eventjs file tries to get the data from the socket and do stuff on the ui, this worked a while ago and I do not remember how I declared the socket variable
// I tried following declarations but none work
var socket = io('http://localhost:3000');
var socket = io('http://123.123.123.123:3000'); // sever ip
var socket = io('https://localhost:3000');
var socket = io('https://123.123.123.123:3000'); // sever ip
socket.on('signed-in-channel:App\\Events\\UserSignedIn', (data) => {
// some stuff
});
How do I declare the socket properly?
Server side code:
var io = require('socket.io').listen(8150);
io.sockets.on('connection', function (socket){
});
Client side code:
var socketIO = io('*.*.*.*:8150');
socketIO.once('connect', function(){
});
On http it's worked on https in same page it not connected.
Searched many examples, but all example for express. I dont create any http server in node.js need only to socket.io work.
When running the client over HTTPS, socket.io is attempting to connect to your server over HTTPS as well. Currently your server is only accepting HTTP connections, the listen(port) function does not support HTTPS.
You'll need to create an HTTPS server and then attach socket.io to it, something like this.
var fs = require('fs');
var options = {
key: fs.readFileSync('certs/privkey.pem'),
cert: fs.readFileSync('certs/fullchain.pem')
};
var app = require('https').createServer(options);
var io = require('socket.io').listen(app);
app.listen(8150);
io.sockets.on('connection', function (socket) {
});
And if you need both HTTP and HTTPS, you can start two servers and attach socket.io to both.
var fs = require('fs');
var options = {
key: fs.readFileSync('certs/privkey.pem'),
cert: fs.readFileSync('certs/fullchain.pem')
};
var httpServer = require('http').createServer();
var httpsServer = require('https').createServer(options);
var ioServer = require('socket.io');
var io = new ioServer();
io.attach(httpServer);
io.attach(httpsServer);
httpServer.listen(8150);
httpsServer.listen(8151);
io.sockets.on('connection', function (socket) {
});
Then on the client side you can determine which port to connect to based on whether the page was accessed over HTTP or HTTPS.
var port = location.protocol === 'https:' ? 8151 : 8150;
var socketIO = io('*.*.*.*:' + port);
socketIO.once('connect', function() {
});
Use letsencrypt with Plesk for a valid SSL certificat.
options = {
key: fs.readFileSync('/usr/local/psa/var/modules/letsencrypt/etc/live/mydomain.com/privkey.pem'),
cert: fs.readFileSync('/usr/local/psa/var/modules/letsencrypt/etc/live/mydomain.com/cert.pem'),
ca: fs.readFileSync('/usr/local/psa/var/modules/letsencrypt/etc/live/mydomain.com/chain.pem'),
rejectUnauthorized: false,
requestCert: true,
agent: false
}
is there any example about secure inter-server communication in node.js?
Is socket.io necessary to realize this or I may use express too?
I would like to realize mutual authentication between two servers.
Thanks to all!!!
This is an example of what i've done; unfortunately it doesn't work...
Server 1
...
var ioc = require('socket.io-client');
var socketc = ioc.connect("https://127.0.0.1:5000", {'force new connection':true, 'secure':true});
...
Server 2
var fs = require('fs');
var https = require('https');
var io = require('socket.io');
var crypto = require('crypto');
// path to private key and certificate
var pathSSH = 'C:\\cert\\';
//array containing private key and certificate
var options = {
key: fs.readFileSync(pathSSH + 'keySatellite.pem'),
cert: fs.readFileSync(pathSSH + 'keySatellite-cert.pem')
};
var port = 5000;
var server = https.createServer(options, function(req, res) {
console.log('Connection with satellite node');
});
var sio = io.listen(server);
// ...listening at port XXXXX
server.listen(port, function(){
console.log("HTTPS server listening on port " + port);
});
sio.sockets.on('connection', function(socket) {
console.log('connected');
socket.disconnect();
});
UPDATE 1
I've noticed that server to server communication works fine if I set a http connection instead of https.
Server:
// Load libraries
var https = require('https');
var fs = require('fs');
var socketio = require('socket.io');
// The server options
var srvAddress = '123.123.123.123';
var srvPort = 8888;
var srvOptions = {
key: fs.readFileSync('ssl/cert.key'),
cert: fs.readFileSync('ssl/cert.crt'),
ca: fs.readFileSync('ssl/cert-ca.crt')
};
// Create a Basic server and response
var app = https.createServer(srvOptions, function(req, res) {
res.writeHead(200);
res.end('Online...');
});
// Create the Socket.io Server over the HTTPS Server
var io = socketio.listen(app, srvAddress);
// Now listen in the specified Port
app.listen(srvPort, srvAddress);
Client:
var socket = require("socket.io-client").connect('https://123.123.123.123:8888', {secure: true});
But client don't connect to server. I tried http instead of https, but the same result.
Server works fine. Without ssl certificate connection works. When try to open via web, then all works too.