0

I'm putting together a website that serves pages, and also has a chat.

The website is built using Express. For the chat, I'd like to use websockets, and for this I was looking into the node:tls module.

Setup:

// https server
import express from 'express';
import https from 'https';

https.createServer({
  key: readFileSync(keyPath),
  cert: readFileSync(certPath),
}, app).listen(3000);

...
// tls server
import { createServer } from 'node:tls';
import fs from 'fs';

createServer({
    key: fs.readFileSync(keyPath),
    cert: fs.readFileSync(certPath),
  },  socket => {
    socket.on('data', ...);
  }).listen(3001);

Is there a way for this setup to use a single server, and a single port? The https server inherits from the tls server, so I guess it could also work with the websocket.

import { connect } from 'node:tls';
const socket = connect({
    host: process.env.RELAY_SERVER_HOST_ORIGIN,
    port: 3000,
  });

^ The above does not seem to be seen by the https server, even with:

https.createServer({
  key: readFileSync(keyPath),
  cert: readFileSync(certPath),
}, (req, res) => {
  console.log(req); // <--- This is never hit
  return app(req, res);
}).listen(3000);

I'm not sure what I am missing here (probably a lot).

qnilab
  • 380
  • 3
  • 18
  • 1
    Are you confusing regular sockets and _web_sockets? The `ws` package can be configured to re-use an exiting Node https server instance/ – Evert Jul 04 '23 at 00:44
  • The webSocket listening server should just use the same https server you're already using for Express. – jfriend00 Jul 04 '23 at 02:13
  • > Are you confusing regular sockets and _web_sockets? I think I am. I feel like I have all that I need with the tls server (long lived connection, encrypted), so I may go with it. I need to read more about websockets, to understand what differentiates them from just sockets. I've seen that it's possible to upgrade from HTTP to websocket here https://nodejs.org/dist/latest-v18.x/docs/api/http.html#event-upgrade, but in the end I am not sure what Websockets would get me that the TLS connection does not. Thanks for asking this question, that was helpful, my question is malformed. – qnilab Jul 04 '23 at 02:53
  • I think this question, its answers and its links may answer it. https://stackoverflow.com/questions/16945345/differences-between-tcp-sockets-and-web-sockets-one-more-time – qnilab Jul 04 '23 at 03:08
  • Ok, I lied a bit with my question. I'm not making a chat, but that was simpler to ask the question. I'm actually doing TCP hole punching. I did manage to make it work, but I did not understand everything I did, now I'm doing a cleanup pass and would like to better understand. WebSockets looks like they are easier to work with (deal with whole messages, no buffer gymnastics to retrieve messages), but they are bound to using port 80 or 443, which is not flexible enough to do hole punching. So, I think I need to keep using simple TCP sockets with TLS encryption. Thanks y'all! – qnilab Jul 04 '23 at 03:14
  • Actually, it looks like https://github.com/websockets/ws allows to specify a port. So it may save from the buffer handling while being flexible enough. I'll keep digging. – qnilab Jul 04 '23 at 03:18

0 Answers0