I am having a hard time finding documentation and examples for the 'newSession' and 'resumeSession' events for the TLS module in node.js.
Any links or help in this direction is appreciated. I need to know more about these events in order to implement a fault resistant tls connection in node.
It's pretty easy: Documentation.
sessionData is a simple object, which you could JSON.stringify(sessionData) and save in a Redis Database. Later on, you can JSON.parse(sessionData) it again and resume the session.
The server itself only needs the session ID (that is sent by the client) to find its session data (if available). If the server can't find the corresponding session data to a session ID, it will start a new session.
/**
* Module dependencies.
*/
var tls = require("tls");
/**
* Initialize a new TLS server.
*/
var opts = {
cert: fs.readFileSync("./ssl/cert.pem")
, key: fs.readFileSync("./ssl/key.pem")
}
, server = tls.createServer(opts).listen(443);
/**
* TLS session management.
*/
var sessions = {};
server.on("newSession", function(sessionId, sessionData) {
sessions[sessionId.toString("hex")] = sessionData;
});
server.on("resumeSession", function(sessionId, callback) {
sessionId = sessionId.toString("hex");
if(sessionId in sessions)
callback(null, sessions[sessionId]); // resume an existing session
else
callback(null, null); // new session will be started
// you could also emit an error, which wil terminate the connection
// callback(new Error("lol wut"));
});
/**
* Request handler.
*/
server.on("request", function(req, res) {
res.end("Hello World");
});