How to connect to Node from Node **with HTTPS**

I've setup a socket server, like so:

/* node-server.js */
var port = 3333;
var fs = require('fs');
var options = {
    key: fs.readFileSync('ssl/server.key'),
    cert: fs.readFileSync('ssl/server.crt')
var server = require('https').Server(options);
var io = require('')(server);
console.log('Socket server listening on port ' + port + '.');
io.sockets.on('connection', function(socket) {
    console.log("Client " + + " connected.");

And a Node client that connects to it:

/* node-client.js */
var url = 'https://localhost:3333';
console.log('Connecting to ' + url);
var io = require('');
var socket = io.connect(url, { reconnection: false });
socket.on('connect_error', function(error){ console.log('Error connecting to ' + url, error);});
socket.on('connect', function() {
    console.log('Connected to ' + url);

However, when trying to connect I get an error { [Error: xhr poll error] description: 503 }.

This error goes away, and everything just works if I remove the "HTTPS" component. Here's a diff showing exactly what I mean.

However, I'm not convinced that HTTPS is the problem, because here is another client (this one in a browser instead of Node) which can connect just fine:

<!DOCTYPE html>
    <script type="text/javascript"
<body onload="io.connect('https://localhost:3333');">

How can I get a Node to connect to a Node over HTTPS?


On Node.js, TLS and HTTPS will validate certificates before accepting them. Therefore, to use self-signed certificates with Node, you will need to set the rejectUnauthorized option when performing requests to false, or use:


The solution was to add this line to the client:


I found that if I manually made HTTPS requests to the socket server:

var https = require('https');
var options = { host: 'localhost',
    port: '3333',
    path: '/',
    method: 'GET',
    { 'User-Agent': 'node-XMLHttpRequest',
    Accept: '*/*',
    Host: 'localhost:3333' },
    agent: false
https.globalAgent.options.rejectUnauthorized = false;
https.request(options, function() {
    throw 'done';

...then requests would fail with the error: DEPTH_ZERO_SELF_SIGNED_CERT, it would seem, was unable to make requests for this reason.

After Googling the error, I found this page.

On that page someone suggested the solution above, which works.

Essentially, I believe it's allowing self-signed certs which may be "insecure".

Need Your Help

Microsoft Solver Foundation - how to use function as constraint?

c# constraint-programming ms-solver-foundation

I'm trying to find a solution to a problem using Microsoft Solver Foundation in C# and I'm having trouble setting up all the constraints I need. My basic model is I have a list of bays and I need t...

WIX downgrade prevention warning is showing at wrong time

user-interface wix upgrade sequence custom-action

I have a strange problem with WIX installer. It has custom action preventing application to install when newer version present:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.