I don't get, how do i configure LEX with socket.io. Here what I have now:
server.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({limit: '50mb', extended: true }));
app.use(bodyParser.json({limit: '50mb'}));
var port = 80;
if(process.argv[2] == 'dev') port = 3001;
var router = express.Router();
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, userId');
next();
});
var api = require('./api');
api(app, port)
app.use(express.static(__dirname + "/../dist"));
app.get('/', function(req, res) {
res.render('index.html');
});
console.log('Magic happens on port ' + port);
api.js
var API = function(app, port){
var pg = require('pg');
var lex = require('letsencrypt-express').testing();
var connectedUsers = {};
var config ={
user: "postgres",
password: "password",
database: "production",
port: 5432,
host: "serverIpAddress",
ssl: false
};
pg.connect(config, function(err, client, done) {
if (err) {
return console.error('error fetching client from pool', err);
}
// var server = require('http').Server(app);
var server = lex.create({
configDir: './letsencrypt.config',
onRequest: app,
letsencrypt: null,
});
var io = require('socket.io')(server);
server.listen(port);
io.on('connection', function (socket) {
console.log('new connection')
});
});
}
module.exports = API
take a look at commented out line in api.js: everything works fine when I use this instead of:
var server = lex.create({
configDir: './letsencrypt.config',
onRequest: app,
letsencrypt: null,
});
With LEX i'm receiving this error trying to run the server:
[LEX] testing mode turned on
[LEX] default server: https://acme-staging.api.letsencrypt.org/directory
###################################################
# #
# Open up a browser and visit this server #
# at its domain name. #
# #
# ENJOY! #
# #
###################################################
Note: testing certs will be installed because .testing() was called.
remove .testing() to get live certs.
[LEX] automatic registration handling turned on for testing.
Magic happens on port 3001
[LEX] creating sniCallback {
"configDir": "./letsencrypt.config",
"onRequest": {
"domain": null,
"_events": {},
"_eventsCount": 3,
"_connections": 0,
"_handle": null,
"_usingSlaves": false,
"_slaves": [],
"_unref": false,
"allowHalfOpen": true,
"pauseOnConnect": false,
"httpAllowHalfOpen": false,
"timeout": 120000
},
"letsencrypt": {
"backend": {}
},
"debug": true,
"webrootPath": "/var/folders/2m/vfw1bgqd4pv7hk86kcbvcrlc0000gn/T/acme-challenge",
"privkeyPath": "./letsencrypt.config/live/:hostname/privkey.pem",
"fullchainPath": "./letsencrypt.config/live/:hostname/fullchain.pem",
"certPath": "./letsencrypt.config/live/:hostname/cert.pem",
"chainPath": "./letsencrypt.config/live/:hostname/chain.pem",
"server": "https://acme-staging.api.letsencrypt.org/directory"
}
projectRootFolder/node_modules/socket.io/node_modules/engine.io/lib/server.js:358
var listeners = server.listeners('request').slice(0);
^
TypeError: server.listeners is not a function
at Server.attach (projectRootFolder/node_modules/socket.io/node_modules/engine.io/lib/server.js:358:26)
at Function.attach (projectRootFolder/node_modules/socket.io/node_modules/engine.io/lib/engine.io.js:124:10)
at Server.listen.Server.attach (projectRootFolder/node_modules/socket.io/lib/index.js:226:21)
at new Server (projectRootFolder/node_modules/socket.io/lib/index.js:51:17)
at Server (projectRootFolder/node_modules/socket.io/lib/index.js:39:41)
at projectRootFolder/server/api.js:73:34
at projectRootFolder/node_modules/pg/lib/pool.js:82:9
at projectRootFolder/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:281:11
at projectRootFolder/node_modules/pg/lib/pool.js:56:18
at null.<anonymous> (projectRootFolder/node_modules/pg/lib/client.js:149:7)
at g (events.js:260:16)
at emitOne (events.js:82:20)
at emit (events.js:169:7)
at Socket.<anonymous> (projectRootFolder/node_modules/pg/lib/connection.js:109:12)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
I guess, I have very simple mistake in my code, because I was not configuring Socket.io with SSL before.
Thank you
Try this code. It only will work with the newest version of letsencrypt-express.
app.js
var express = require('express');
var app = express();
var socketIO = require('socket.io');
var LEX = require('letsencrypt-express').testing();
var https = require('http2');
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
var lex = LEX.create({
configDir: '/etc/letsencrypt'
, letsencrypt: null
, approveRegistration: function (hostname, cb) {
cb(null, {
domains: ['example.com']
, email: '[email protected]'
, agreeTos: true
});
}
});
var server = https.createServer(lex.httpsOptions, LEX.createAcmeResponder(lex, app));
server.listen(443);
var io = socketIO.listen(server);
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
index.html
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
var socket = io.connect('https://example.com');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With