rayaqin
rayaqin

Reputation: 402

How can I create an SSL connection for socket.io in nest.js?

|Greetings|

We are developing an application using nest.js and socket.io, and I'd like to know whether it's possible to create an SSL connection for this environment.

Here's the link to the repo: https://github.com/nokia/skilltree ( the latest attempts have been made in the David branch )

I tried this one, but the socket.io still doesn't use SSL connection: https://blog.cloudboost.io/everything-about-creating-an-https-server-using-node-js-2fc5c48a8d4e They suggest this:

var options = {
  key: key,
  cert: cert,
  ca: ca
};
var https = require('https');
https.createServer(options, app).listen(443);

Thank you for any help in advance

Upvotes: 3

Views: 6042

Answers (2)

jlang
jlang

Reputation: 1057

Nest takes an option object as second parameter, which also contains https options, like:

const app = await NestFactory.create(AppModule, { 
    httpsOptions: {
      key: 'key',
      ca: 'ca',
      cert: 'cert',
    },
  });

await app.listen(3000);

So there should be no need to create the express instance yourself. Haven't tested, but it should actually work. :) See also: HttpOptions Interface NestJs

Upvotes: 4

Andrew Gura
Andrew Gura

Reputation: 382

Spent entire day with exactly the same issue, here the best solution I could find:

const httpsOptions = {
    key: key,
    cert: cert,
    ca: ca
};
const expressInstance: express.Express = express();
const app: NestApplication = await NestFactory.create(
    MainModule, 
    expressInstance, 
    { httpsOptions }
);
await app.listen(Environment.PORT);

With this approach secure websockets work just fine for me

Upvotes: 1

Related Questions