Follow-up of: API connection via ipv6 vs. ipv4 - support - Discourse Meta
I recently upgraded Node.js from 18 to 20 and my application suddenly stopped getting API response back from Discourse. It took me a while to figure out it was related to the above issue. I verified this by making a connection only over IPv4 like:
import {Agent} from 'node:https'
import axios from 'axios'
import {env} from 'node:process'
export const axiosDiscourse = axios.create({
baseURL: 'https://<discourse-url>',
headers: {
'api-key': env['DISCOURSE_KEY'],
'api-username': env['DISCOURSE_USERNAME']
},
httpsAgent: new Agent({
family: 4
})
})
the important part being specifying the HTTP Agent - I was using it without that before. This now works, but I was in an attempt to migrate off Axios, so I can use other fetch-based libraries like Wretch (primarly to reduce the bundle size by using that in the frontend as well): elbywan/wretch: A tiny wrapper built around fetch with an intuitive syntax. (github.com). However, there’s no option to specify the IPv4 or IPv6 with fetch. Thus, I figured I need to either:
- Force using IPv4 only somehow and in spite of all my attempts so far, this has not been possible
- Check with Discourse on how I could get this working on IPv6.
I am connecting to various other APIs from my application, none have this issue, it’s only the connection with Discourse that’s failing.
This is the error I’m getting in case it helps:
Error: connect ETIMEDOUT 184.105.99.43:443
at createConnectionError (node:net:1634:14)
at Timeout.internalConnectMultipleTimeout (node:net:1685:38)
at listOnTimeout (node:internal/timers:575:11)
at processTimers (node:internal/timers:514:7) {
errno: -110,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '184.105.99.43',
port: 443
},
Error: connect ENETUNREACH 2602:fd3f:3:ff01::2b:443 - Local (:::0)
at internalConnectMultiple (node:net:1176:40)
at Timeout.internalConnectMultipleTimeout (node:net:1687:3)
at listOnTimeout (node:internal/timers:575:11)
at processTimers (node:internal/timers:514:7) {
errno: -101,
code: 'ENETUNREACH',
syscall: 'connect',
address: '2602:fd3f:3:ff01::2b',
port: 443
}
What’s interesting is, unlike the above mentioned thread, I don’t get response after long. My request immediately fails with this error. The API call works fine in the browser, curl and anything else I can test.
My way of making a request to the API doesn’t matter as long as I’m making a request from my application. It simply fails and works only by using the family option as mentioned above. In the linked thread, I see this being mentioned:’
You’ll need to investigate why IPv6 connections don’t work on your network.
I have 2 concerns with that:
- I don’t know how to specifically check that.
- Not sure why this would be a problem only with Discourse, and only with my application.