crystal
crystal copied to clipboard
macOS Monterey uses port 5000 for new AirPlay service by default
Heads up, in the new OS macOS Monterey they added a new service used for AirPlay that listens on port 5000
by default. Not a huge deal to change the port for PostGraphile using --port
but might be a good idea to change the default from 5000
as it's confusing at first when you get address already in use error. Apple obviously is not going to change from using port 5000
for their AirPlay service.
See https://developer.apple.com/forums/thread/682332 for background.
le sigh
Changing this would be a breaking change. What we can do, however, is if --port
is not specified we can try and bind to 5000 and if that fails we could bind to a random port number (i.e. port 0
) and let the user know.
This would be a relatively straightforward change for someone to make if they wanted to, we already figure out what the actual port was for rendering in the welcome message:
https://github.com/graphile/postgraphile/blob/619efbcf38de41bf59ccd6ca508f8ebdafa88ea1/src/postgraphile/cli.ts#L852
So effectively you'd need to take the existing listen callback and extract it to its own dedicated function handleListening
:
https://github.com/graphile/postgraphile/blob/619efbcf38de41bf59ccd6ca508f8ebdafa88ea1/src/postgraphile/cli.ts#L850
Then you'd add a server.once('error', ...)
handler just before the server.listen(port, hostname, handleListening)
call that would try again via server.listen(0, hostname, handleListening)
.
If port is omitted or is 0, the operating system will assign an arbitrary unused port, which can be retrieved by using server.address().port after the 'listening' event has been emitted. -- https://nodejs.org/dist/latest-v16.x/docs/api/net.html#serverlistenport-host-backlog-callback
The only semi-complex part I think would be determining whether or not the --port
option was specified - if it was then I wouldn't want this fallback mechanic. It's only suitable to fallback if --port
was not specified on the CLI. Determining this is left as an exercise to the reader :wink:
I suggest adding some notice about this as I was puzzled why I am getting 403 when trying to reach /graphiql
This was driving me crazy. Google search does not produce any helpful results. Glad to finally have found the answer!
@reisr3 If you can remember (or find out) what any of your google searches were for, perhaps you could add them in a reply to this thread to help future people find it?
I think it would be good to detect if port is already taken and fail based on that, that would be clear.
BTW can many apps be bound to single port? I though they can if protocol is different but last time I did this was long time ago.
No, not unless they bind to specific (and unique) hosts. e.g. one can bind to 127.0.0.1:80
and another to 127.0.0.2:80
but they can't both bind to 0.0.0.0:80
Just ran into this myself and wasted an hour trying to figure out why http://localhost:5000/graphiql was failing with a 403 and no other explanation. Switching "localhost" to "127.0.0.1" fixed the problem, but I imagine many others will stumble upon this issue too.
I've added a note to the usage-cli
docs, and to the README. Hopefully this helps.
Great work @benjie, that was really handy! Unless I'm missing something, you may also need to support the port option when using via Library instead of CLI.
The library doesn't bind to a port on its own, that's the responsibility of whatever webserver you're using. The examples use port 3000 :man_shrugging:
The library doesn't bind to a port on its own, that's the responsibility of whatever webserver you're using. The examples use port 3000 🤷♂️
Gotcha! Totally makes sense and I did not realize that until now 🤦. Confirmed that it's working as expected!
The default port number in V5 is 5678