Chapter 12: Running app from physical device gives error "Connection refused"

When running the app on the simulator everything works, but when I try and run the code from my iPhone 11 Pro, I get the following error.

2020-10-10 13:13:00.698589+0200 TILiOS[995:84641] [connection] nw_socket_handle_socket_event [C1.1:2] Socket SO_ERROR [61: Connection refused]

2020-10-10 13:13:00.701632+0200 TILiOS[995:84641] [connection] nw_socket_handle_socket_event [C1.2:2] Socket SO_ERROR [61: Connection refused]

2020-10-10 13:13:00.703308+0200 TILiOS[995:84641] Connection 1: received failure notification

2020-10-10 13:13:00.703373+0200 TILiOS[995:84641] Connection 1: failed to connect 1:61, reason -1

2020-10-10 13:13:00.703404+0200 TILiOS[995:84641] Connection 1: encountered error(1:61)

2020-10-10 13:13:00.711549+0200 TILiOS[995:84674] Task <5D3446D3-8916-4F0E-B1C5-91CDE4C67C33>.<1> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])

2020-10-10 13:13:00.717426+0200 TILiOS[995:84674] Task <5D3446D3-8916-4F0E-B1C5-91CDE4C67C33>.<1> finished with error [-1004] Error Domain=NSURLErrorDomain Code=-1004 “Could not connect to the server.” UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x280bc3180 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 “(null)” UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <5D3446D3-8916-4F0E-B1C5-91CDE4C67C33>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(

“LocalDataTask <5D3446D3-8916-4F0E-B1C5-91CDE4C67C33>.<1>”

), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://localhost/api/acronyms, NSErrorFailingURLKey=http://localhost/api/acronyms, _kCFStreamErrorDomainKey=1}

I’m guessing this could be because running from my phone “localhost” is not accessible but changing my baseURL to my machines ip: “http://192.168.1.7:8080” does not work. Also tried adding “Allow Local Networking: Yes” in Info.p list but found no joy.

Running:
macOS Catalina v.10.15.7
Xcode: v.12.0.1
Docker: 2.4.0.0

This is not a show stopper but I would really love to figure it out and any help would be appreciated?

Hey so the reason is that by default Vapor binds to localhost which only allows connections from that machine. So when running in the simulator it’s all fine because it’s the same machine. To allow connections from external IP addresses, you need to set the hostname. The easiest way to do this is in configure.swift with:

app.http.server.configuration.hostname = "0.0.0.0"

Hi Tim! I tried your solution and still can’t connect using an iPhone. I can list the acronyms using GET “http://0.0.0.0:8080/api/acronyms” in RapidAPI (RESTed alternative), but I get an “about:blank” page instead of the raw JSON when typing the same URL in a web browser. Any idea how to fix this in 2025 using Swift 6, maOS 15.2 Sequoia & Vapor 4? Thanks!

0.0.0.0 is a special broadcast address. You should use the IP address of you Mac that’s running the Vapor app instead

You don’t want to do step 2 - it should stay as 0.0.0.0 - this tells the app to listen to requests from all IP addresses. If you specify an IP address it will only accept requests from that IP address. If you provide nothing then it defaults to localhost which may be the issue you were hitting

Tim, thank you for taking the time to help newbies like myself. Specially in these transition times, as you’re still hard at work on Vapor 5.

I’ve followed all your suggestions and this is what finally worked for me:

  • Step 1 - Get your Mac’s IP address from the Terminal, using ifconfig:
% ifconfig -l | xargs -n1 ipconfig getifaddr
123.123.0.123
  • Step 2 - Use the special broadcast address 0.0.0.0 to define the hostname in TILApp / configure.swift:
app.http.server.configuration.hostname = "0.0.0.0"
app.http.server.configuration.port = 8080
  • Step 3 - Use the IP address to define the baseURL constant in TILiOS / ResourceRequest.swift.
let baseURL = "http://123.123.0.123:8080/api/"
let resourceURL: URL
  • Step 4 - Use the IP address (or 127.0.0.1, the machine’s localhost) to query the app from a REST editor. For example, the following requests should both list the users currently in the database:
    GET http://123.123.0.123:8080/api/users
    GET http://127.0.0.1:8080/api/users