Node + SSL = SLOW - node.js

I have set up a node.js-server for running a chat service used by our site. It works. However some customers are NOT able to connect (is done automatically via javascript). They never pop up in the list of connected users (in ie 7,8,9).
My site is running on https (port 443) and therefore my node.js-server is also running on SSL (port 8443) (with the same certificate).
I am using Node 0.6.20 (have tried numerous other versions). I have the following package-setup:
{
"name":"My Chat",
"description":"Chat app using socket.io",
"version":"0.0.1",
"dependencies":{
"express":"3.x.x",
"socket.io":"~0.8.7"
},
"engines":{"node":"0.6.20"}
}
and my node server looks like this:
var fs = require('fs');
var express = require('express');
var https = require('https');
var sio = require('socket.io');
var https_options = {
pfx: fs.readFileSync('cert/certificate.pfx'),
passphrase: "password"
};
var PORT = 8443;
var HOST = '0.0.0.0';
var myArray = new Object();
var userArray = {};
var nicknames = {};
app = express();
app.use(app.router);
server = https.createServer(https_options, app).listen(PORT, HOST);
console.log('HTTPS Server listening on %s:%s', HOST, PORT);
var io = sio.listen(server);
io.set("transports", [ 'websocket'
, 'flashsocket'
, 'htmlfile'
, 'polling'
, 'xhr-polling'
, 'jsonp-polling']);
// routes
app.get('/hey', function(req, res) {
res.send('HEY!');
});
app.post('/ho', function(req, res) {
res.send('HO!');
});
io.sockets.on('connection', function (socket) {
});

Related

Socket.IO - Server doesn't send response to client under SSL

I'm comfronted with a big problem with socket.io ans NodeJS.
I try to communicate with the NodeJS server through Socket.IO under HTTPS, but when it's time for the server to send a response to the client, it doesn't do anything.
I've checked, but it's not a certificate error ! Key and certificate are valid.
However, without HTTPS, it works perfectly.
Here is my code:
var express = require('express'),
fs = require('fs');
var config = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8')),
options = config.options || {};
var PORT = options.port,
SSL_PORT = options.ssl_port;
var app = express(),
server = require('http').createServer(app);
io = require('socket.io').listen(server);
server.listen(PORT, function()
{
startServer(io);
});
if (options.ssl_key.length && options.ssl_cert.length)
{
var sslOpt = {
key: fs.readFileSync(options.ssl_key, 'utf8'),
cert: fs.readFileSync(options.ssl_cert, 'utf8')
};
var sslServ = require('https').createServer(sslOpt, app),
ioSsl = require('socket.io').listen(sslServ);
sslServ.listen(SSL_PORT, function()
{
startServer(ioSsl);
});
}
function startServer(io)
{
io.on('connection', function(socket)
{
socket.on('msg', function(data)
{
io.emit('clientMsg', 'New message !');
});
}.bind(this));
}
And the client part:
var PORT = 2999,
secured = false,
serverName = 'my-website-url',
completeUrl = 'https://my-website-url.com';
if (completeUrl.substr(0, 5) == 'https')
{
PORT = 3000;
secured = true;
}
var socket = io(serverName + ':' + PORT, {
transports: ['websocket'],
secure: secured
});
socket.emit('msg', 'I send a msg');
I use port 2999 for HTTP and 3000 for HTTPS, when I send a message through the HTTPS server (port 3000), I don't receive the server response, but I receive it on HTTP server (port 2999).
So, under SSL, the server doesn't send a response to clients who are using my HTTPS website.

socket.io can't connect to server

I'm hosting my application on openshift. I am using a custom domain. And socket.io wasn't able to download the client side script so I just used the cdn instead. But now it's not able to connect to a namespace. These are the errors it is giving me on the console log
This is my client side code on the .html page to download the client side script
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
and the .js index page to connect to the index namespace
var socket = io("http://www.loomius.com/index");
Here is my server side code
var express = require('express');
var app = express();
var http = require('http');
var io = require('socket.io')(http);
var mongoose = require('mongoose');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var https = require('https');
// listening on the port
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
});
First instead of
var socket = io("http://www.loomius.com/index");
use this to solve issue with a need of cdn
var socket = io.connect("/");
Then on server use this instead
http = http.createServer( app ).listen( process.env.PORT, process.env.IP || "0.0.0.0", function() { // or define ip and port manually
var io = require( 'socket.io' )( http );
io.on('connection', function( socket ) {
// add event listeners here
}
});

Nodejs - Socketio not working with https server, what could be the reason?

I tried debugging using http server, and socket.io worked, however when I try to use https socket.io stopped working
// create server for http and https
//var httpserver = http.createServer(app).listen('3000', '127.0.0.1');
var https_server = https.createServer(options, app).listen('4000', '127.0.0.1');
// let the backend listens to this server
UserServer.listen(https_server);
//UserServer.listen(httpserver);
exports.listen = function(server){
io = socketio.listen(server);
io.sockets.on('connection', function(socket){
console.log("Socket is connected, sweet ");
});
}
When
var httpserver = http.createServer(app).listen('3000', '127.0.0.1');
UserServer.listen(httpserver);
Print out
Socket is connected, sweet "
When
var https_server = https.createServer(options, app).listen('4000', '127.0.0.1');
UserServer.listen(https_server);
There are no output
Here are my set up for https
var express = require('express'),
https = require('https'),
fs = require('fs');
var options = {
key: fs.readFileSync('./key.pem', 'utf8'),
cert: fs.readFileSync('./cert.pem', 'utf8')
};
I was able to display the https page, so I am guessing https setup was not the problem
Any advice would be appreciated, thanks
First of all, you need to run Node.js Server with SSL.
Server.js
var fs = require('fs');
var options = {
key: fs.readFileSync('ssl/private/domain.com.key'),
cert: fs.readFileSync('ssl/certs/domain.com.crt'),
ca: fs.readFileSync('ssl/certs/domain.com.cabundle')
};
// You can do this easily if you are on windows
// var options = { pfx: fs.readFileSync('/home/mydir/secure.pfx'); };
var app = require('https').createServer(options, handler),
io = require('socket.io').listen(app);
//Recommended Production settings
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('flash policy port', 3300); //==> override Flash Policy Port
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
function handler(req, res) {
res.writeHead(200);
res.end("welcome sir!");
};
io.sockets.on('connection', function (socket) {
socket.on('message', function (data) {
socket.broadcast.emit('message', data);
});
});
app.listen(3300);
Client.js
//socketAddress is server uri ex. https://www.domain.com:3300
var socket = io.connect(socketAddress, {'flash policy port':3300} );
socket.on('connect', function (data) {
//....
});
socket.on('message', function (data) {
//....
});

Secure inter-server communication with node.js

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.

How to connect Two Socket.io Node Application using Socket.io-client in Node js

In my application i need to connect two socket.io node applications.Using socket.io-client we can do like this.But i don't know how socket.io-client works and where to include that.
First Node Application
var express = require('express')
, http = require('http');
var app = express();
app.use(function (req, res) {
app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
io.sockets.on('connection',function(socket){
socket.on('eventFiredInClient',function(data){
socket.emit('secondNodeAppln',data);// i need to get this event in my 2nd node application how can i do this by using socket.io-client
});
});
Second Node Application
var express=require('express');
var http=require('http');
var app=express();
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var serverAddress = '127.0.0.1';
var serverPort = 3000; //first node appln port
var clientio = require('socket.io-client');
var socket = clientio.connect(serverAddress , { port: serverPort });
socket.on('connect', function(){
console.log('connected');
});
socket.on('disconnect', function(){
console.log('disconnected');
});
var io = require('socket.io').listen(server);
server.listen(6509);
//here i need to get the 'secondNodeAppln' event raised in first node application.How can i do this.
You need to create a socket.io client in your first app:
var io = require('socket.io').listen(server); // this is the socket.io server
var clientio = require('socket.io-client'); // this is the socket.io client
var client = clientio.connect(...); // connect to second app
io.sockets.on('connection',function(socket) {
socket.on('eventFiredInClient',function(data) {
client.emit('secondNodeAppln', data); // send it to your second app
});
});
And in your second app, just listen for those events:
io.sockets.on('connection', function (socket) {
socket.on('secondNodeAppln', function(data) {
...
});
});
There's a bit of a race condition because the code above doesn't wait for a connect event on the client socket before passing events to it.
EDIT see this gist for a standalone demo. Save the three files to a directory, start the servers:
node serverserver &
node clientserver
And open http://localhost:3012 in your browser.

Resources