Looking for an example of how to setup HTTPS with greenlock-express in combination with a secure websocket server.
Asked
Active
Viewed 1,081 times
1 Answers
3
Here's how I wound up setting this up. The key was to use the tslOptions generated by greenlock-express to setup an HTTPS server manually, and then attach a Websocket Server to it in the normal way. With this approach, the redirect from HTTP to HTTPS has to be done manually.
I initially couldn't get things to work because I hadn't opened up port 443 on my server. Make sure you do that otherwise HTTPS won't work!
const express = require('express');
const http = require('http');
const https = require('https');
const WebSocket = require('ws');
//EXPRESS TO BUNDLE APP
let my_app = express();
let dir = __dirname + '/../app';
io_app.use(express.static(dir));
//Just serving static files from a sibling directory called /app
//// SETUP HTTP GREENLOCK
let greenlock = require('greenlock-express').create({
// Let's Encrypt v2 is ACME draft 11
version: 'draft-11'
,
server: 'https://acme-v02.api.letsencrypt.org/directory'
// Note: If at first you don't succeed, switch to staging to debug
// https://acme-staging-v02.api.letsencrypt.org/directory
// You MUST change this to a valid email address
,
email: '[email protected]'
// You MUST NOT build clients that accept the ToS without asking the user
,
agreeTos: true
// You MUST change these to valid domains
// NOTE: all domains will validated and listed on the certificate
,
approveDomains: ['example.com', 'www.example.com']
// You MUST have access to write to directory where certs are saved
// ex: /home/foouser/acme/etc
,
configDir: require('path').join(require('os').homedir(), 'acme', 'etc')
// Join the community to get notified of important updates and help me make greenlock better
,
communityMember: true
// Contribute telemetry data to the project
,
telemetry: true
,
debug: true
});
//// REDIRECT HTTP TO HTTPS
let redirectHttps = require('redirect-https')();
let acmeChallengeHandler = greenlock.middleware(redirectHttps);
http.createServer(acmeChallengeHandler).listen(80, function() {
console.log("Listening for ACME http-01 challenges on", this.address());
});
//// HTTPS SERVER + WEBSOCKETS
let server = https.createServer(greenlock.tlsOptions, my_app);
let ws = new WebSocket.Server({
server
});
ws.on('connection', function(ws, req) {
//websocket on connection...
});
server.listen(443);

Mischawaka
- 225
- 2
- 10
-
Thanks for that, add a websocket example to https://git.coolaj86.com/coolaj86/greenlock-express.js/src/branch/master/examples/websockets.js – coolaj86 Jul 03 '18 at 09:31
-
@CoolAJ86, Can you add an example for socket.io as well ? var server = lex.listen(80, 443); var io = require('socket.io')(server); – Koder Sep 21 '18 at 15:57
-
@Koder Want to make a PR based on one of the existing examples? – coolaj86 Sep 22 '18 at 04:06
-
@coolaj86 the link to the "official" example does not work anymore. I get a 404. – vidstige Mar 23 '21 at 09:14
-
2@vidstige Just moved over one directory: https://git.coolaj86.com/coolaj86/greenlock-express.js/src/branch/master/examples/websockets/server.js – coolaj86 Mar 24 '21 at 13:39