1

I'm asking if it is possible to create a backend in node.js who has the same API but is compatible both TCP and WebSocket protocols, on the same port ? I'm aware that Websocket is TCP based but have some high-level pre-requisites and found nothing on the subject.

Thanks all

Somebody
  • 169
  • 2
  • 2
  • 11

1 Answers1

1

This might be possible depending on the protocol, but it's going to be a hassle. You'll have to hook the HTTP server (which web sockets sit on top of) and shuffle the data off to a separate handler if it appears to be invalid HTML.

This is going to be error prone. HTTP has a lot of edge cases you would need to accommodate. The alternative protocol is going to have to either be different enough to quickly identify, or actually compatible with HTTP so it can have its own protocol upgrade path.

Related: Overriding Node.js HTTP parser

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Thanks for your point of view. That means you can call a HTTP Server with a websocket instance ? Then create custom handlers to create responses. – Somebody Nov 27 '18 at 01:01
  • 1
    @Somebody No. Websockets are built on top of HTTP. Any websocket client also has an underlying HTTP client (at least to some extent). You have to make an HTTP upgrade request before getting to websockets. – Brad Nov 27 '18 at 01:02
  • Thanks again for your reply. So I have to make a HTTP upgrade from HTTP to Websocket protocol. If I refer to IANA register : https://www.iana.org/assignments/http-upgrade-tokens/http-upgrade-tokens.xml I can't make a http upgrade from HTTP to TCP ? – Somebody Nov 27 '18 at 01:05
  • @Somebody No! HTTP sits on top of TCP. I recommend getting out a packet sniffer like Wireshark and spending some time understanding the layers at play here. – Brad Nov 27 '18 at 01:08
  • I know that protocols are not the same at all and based on tcp / udp. You give me the answer to get the same code from http to WebSocket (create some logic to detect WebSocket request then make an HTTP upgrade) but can't understand the trick to have the code which respond to a tcp client with the same code / port. – Somebody Nov 27 '18 at 01:11
  • What I'm suggesting is that when the TCP connection is established, before handing data off to the HTTP server layer, intercept it and manipulate as necessary. – Brad Nov 27 '18 at 01:12
  • Thanks again. But it assumes that you have one TCP server with his own port, then another websocket server with his own port. Both on this servers manipulate request as necessary to transform it as an HTTP request to get the same code, that's it ? – Somebody Nov 27 '18 at 01:15
  • 1
    @Somebody No. In every web socket server, there's an underlying HTTP server. In every HTTP server, there's an underlying TCP server. What I'm saying is that you make a TCP server and forward data (internally, in your application) to the upper layers (HTTP, Web Socket) if your code detects an HTTP request. If your code detects whatever this other protocol is, you handle it as you would that protocol. Again, this isn't a great idea for the reasons I outlined in my answer. A much better solution is to not attempt it at all. – Brad Nov 27 '18 at 01:17
  • 1
    All is clear for me now mate. A big thank for your time, I Think I will not attempt it at all, but was for theoretical conception. – Somebody Nov 27 '18 at 01:18