Socket.io server with https - node.js

Here is my socket.io server.js file.
var fs = require('fs');
var https = require('https');
var httpsServer = https.createServer({
key:fs.readFileSync("/etc/nginx/ssl/example.com/server.key").toString(),
cert:fs.readFileSync("/etc/nginx/ssl/example.com/server.crt").toString(),
});
httpsServer.listen(9091);
io = require('socket.io').listen(httpsServer);
I run node server.js on server and I tried to go to here:
https://example.com:9091/socket.io/socket.io.js
But it didn't work. (This site can't be reached)
When I did it like as following with http, it worked well.
(http://example.com:9091/socket.io/socket.io.js)
var http = require('http');
var httpServer = http.createServer();
httpServer.listen(9091);
io = require('socket.io').listen(httpServer);
I know we can configure it with nginx reverse proxy too but
what I want to is to setup it without nginx reverse proxy.
Can anyone help me? Thanks in advance.

Related

node.js socket io https

I'm trying to establish a WebSocket (with node.js/socket.io) on an
HTTPS protocol. But the client still keeps on polling and could not find the server although the server seems fine and listens.
This is what I have done so far. Could you see anything wrong with it?
My assumption is there is something not right with the certificates which I am using. I encrypted the server with plesk "lets encrypt" and took the certificates from this procedure is that right?
---- server side ----
var fs = require('fs');
var express = require('/opt/plesk/node/7/bin/node_modules/express');
var https = require('https');
var app = express();
var server = https.createServer({
key: fs.readFileSync('file.pem'),
cert: fs.readFileSync('file.crt')
},app);
var io = require('/opt/plesk/node/7/bin/node_modules/socket.io').listen(server);
server.listen(8080);
---- client side -----
var socket = io('/', {rejectUnauthorized: false, secure:true});
This is what the client gives me continuously:
https://foo.de:8080/socket.io/?EIO=3&transport=polling&t=MF9zjE6
Since you don't tell what error message you get it's a little hard to identify your problem. Perhaps you should try getting your socket to work and then afterwards try to implement https.
Maybe you should try and simplify it a bit like this.
Server:
var express = require('express')
var app = express()
var server = require('http').Server(app)
var io = require('socket.io')(server)
server.listen(8080);
Client:
var socket = io();
This piece of code should create your socket and your express app. Then you could try and change http to https and it should work aswell.

How to get certificate file from server?

When i dont get any proper answer so i asked this question that i created a chat app for my site using socket.io and node.js my site is on https server it's not working it's giving me error
GET https://example.com:3000/socket.io/?EIO=3&transport=polling&t=1507034613131-2 net::ERR_INSECURE_RESPONSE
CODE IS HERE
var app = require('express')();
var fs = require('fs');
var path = require('path');
var forceSsl = require('express-force-ssl');
app.use(forceSsl);
var options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
var server = require('https').createServer(options, app).listen(3000,function(){
console.log("Https server started on port 3000");
});
var io = require('socket.io').listen(server);
i find above code for https authentication so how can i get these files i have already installed ssl.
i think you can check in your cpanel there is ssl manager which is cpanel service so you can get from there after that you can use in your https nodejs service

How do I listen on port 443 using Express?

var app, certificate, credentials, express, fs, http, httpServer, https, httpsServer, privateKey;
fs = require('fs');
http = require('http');
https = require('https');
privateKey = fs.readFileSync('key.pem', 'utf8');
console.log(privateKey);
certificate = fs.readFileSync('cert.pem', 'utf8');
console.log(certificate);
credentials = {
key: privateKey,
cert: certificate
};
express = require('express');
app = express();
httpServer = http.createServer(app);
httpsServer = https.createServer(credentials, app);
httpServer.listen(80);
httpsServer.listen(443);
I am on OS X and I have confirmed nothing else is listening on 80 and 443. I run this as sudo and when I go http://127.0.0.1, it works. However, when I go to https://127.0.0.1, I get not found.
What am I doing incorrect?
To enable your app to listen for both http and https on ports 80 and 443 respectively, do the following
Create an express app:
var express = require('express');
var app = express();
The app returned by express() is a JavaScript function. It can be be passed to Node’s HTTP servers as a callback to handle requests. This makes it easy to provide both HTTP and HTTPS versions of your app using the same code base.
You can do so as follows:
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();
var options = {
key: fs.readFileSync('/path/to/key.pem'),
cert: fs.readFileSync('/path/to/cert.pem')
};
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
For complete detail see the doc
add the following line of code:
app.listen(443);
Also, try getting rid of all of the http module, as express handles most of that for you. Look at the beginning Hello World for Express, http://expressjs.com/starter/hello-world.html and look for the part where it handles the port.

http.createServer(app) v. http.Server(app)

On the socket.io web page, Get Started: Chat application, located here:
http://socket.io/get-started/chat/
there is this code:
var app = require('express')();
var http = require('http').Server(app);
which could be rewritten a little more clearly like this:
var express = require('express');
var http = require('http');
var app = express();
var server = http.Server(app);
The socket.io example uses http.Server() to create a server. Yet, the express docs for app.listen() show an example where the server is created using http.createServer(app):
app.listen()
Bind and listen for connections on the given host and port. This
method is identical to node's http.Server#listen().
var express = require('express');
var app = express();
app.listen(3000);
The app returned by express() is in fact a JavaScript Function,
designed to be passed to node's HTTP servers as a callback to handle
requests. This allows you to provide both HTTP and HTTPS versions of
your app with the same codebase easily, as the app does not inherit
from these (it is simply a callback):
var express = require('express');
var https = require('https');
var http = require('http');
var app = express();
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
The app.listen() method is a convenience method for the following (if
you wish to use HTTPS or provide both, use the technique above):
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
What's the difference between http.createServer(app) and http.Server(app)?? The http docs are no help.
There is no difference. http.createServer() only does one thing: it calls http.Server() internally and returns the resulting instance.

HTTPS with nodejs and connect

I'm currently using nodejs with connect as my HTTP server. Is there anyway to activate HTTPS with connect? I cannot find any documentation about it. Thanks.
Herry
Instead of creating http server, use https server for connect :
var fs = require('fs');
var connect = require('connect')
//, http = require('http'); Use https server instead
, https = require('https');
var options = {
key: fs.readFileSync('ssl/server.key'),
cert: fs.readFileSync('ssl/server.crt'),
ca: fs.readFileSync('ssl/ca.crt')
};
var app = connect();
https.createServer(options,app).listen(3000);
See the documentation for https here and tls server (https is a subclass of tls) here
From http://tjholowaychuk.com/post/18418627138/connect-2-0
HTTP and HTTPS
Previously connect.Server inherited from Node’s core net.Server, this
made it difficult to provide both HTTP and HTTPS for your application.
The result of connect() (formerly connect.createServer()) is now
simply a JavaScript Function. This means that you may omit the call to
app.listen(), and simply pass app to a Node net.Server as shown here:
var connect = require('connect')
, http = require('http')
, https = require('https');
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(function(req, res){
res.end('hello world\n');
})
http.createServer(app).listen(80);
https.createServer(tlsOptions, app).listen(443);
The same is true for express 3.0 since it inherits connect 2.0

Resources