Nestjs HTTP + Websocket servers

Hello folks and thank you for the amazing work done in the platform :pray: ,

I’m reaching out to you today because I’m building a Nestjs application that is composed of:

  • An HTTP server on port 4000
  • A WS server on port 4001

In order to request the HTTP server, I’m sending requests like https://nestjs-server.com and in order to connect to the WS server, I would like to connect to wss://nestjs-server.com.

My problem is that I don’t find a way to redirect the requests coming from the WSS protocol to the internal port 4001.

Right now, with my actual configuration, it’s (hopefully) not working as expected since the request would be routed to the 4000 port:

[[services]]
  http_checks = []
  internal_port = 4000
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

Do you have an idea how I can tackle this (except by creating a dedicated app for my websocket server :sweat:)

Thank you so much and have a good one :blush:

Are both the http and websocket server using the same codebase or are they different projects / frameworks?

Normally you don’t need a dedicated port for websockets: normal http requests can be upgraded to a websocket connect.

I’m not sure if this is the “gateway” thing from nestjs, but looking at the docs they specifically mention it should listen on the same port: Documentation | NestJS - A progressive Node.js framework

In general, each gateway is listening on the same port as the HTTP server , unless your app is not a web application, or you have changed the port manually.

ooooh dang :man_facepalming: . So sorry to have posted without having fully read the documentation…

I’ve updated my project and it works seemlessy now. Thank you so much for the help and again, sorry for having bothered you :face_holding_back_tears:

2 Likes

No problem! I’m glad it’s working now :slight_smile:

1 Like