Unable to proxy connection to websocket

I’m running two apps:

  • A web app + proxy combo in one app. The proxy is in front of the web app and forwards any requests to /api to the api server and all other requests get forwarded to the web app.
  • The api server: simple node express/ws server listening on /api

My API server will handle http requests as well as websocket requests.

Currently, the proxy forwards http requests just fine, but fails to forward the websocket requests, they just hang indefinitely

You can see the behavior here, using this to test the wss:// urls:

Accessing API from web app:
https://new.ayoubd.com/api/hello (works)
wss://new.ayoubd.com/api (not working)

Accessing API directly:
https://api.ayoubd.com/api/hello
wss://api.ayoubd.com/api (works, receives message every 1s)

This all works locally in dev when I run both servers, and also when I run the site locally and point my local proxy directly to api.ayoubd.com, which leads me to think this has something to do with specifically my fly.io deployment. Does anyone have any ideas about what’s going on?

Site fly.toml:

app = ...
primary_region = ...

[build]
  dockerfile = "Dockerfile"

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

[[vm]]
  size = "shared-cpu-1x"
  memory_mb = 512

api server fly.toml:

app = ...
primary_region = ...

[build]
  dockerfile = "Dockerfile"

[http_service]
  internal_port = 3001
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

[[vm]]
  cpu_kind = "shared"
  cpus = 1
  memory_mb = 256

Proxying a websocket requires special handling. I’ve done it with Apache httpd and nginx, but I haven’t tried it with node. I’ll include two pointers below that work with Node.js, but first perhaps a simpler solution.

If you respond with a fly-replay header, specifying the name of the app you want to handle this request, there is no need to proxy at all - the request will be routed to the app and all subsequent websocket data will proceed directly between the API app and the client.


Here’s an example with express:

Here’s an example with middleware:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.