Launchd daemon and WebSockets

We are developing a launchd daemon that will communicate with a remote server via a WebSocket. On 10.15 we are using the native WebSocket API NSURLSessionWebSocketTask. The daemon opens the WebSocket connection to the remote server and can send a message to the remote server and get a reply back. The problem we are encountering is that the remote server can not send a messages to the daemon over the WebSocket if the server is the one initiating the communications. The daemon never receives those messages.


What is really strange is that if we move the exact same code into a Application the WebSocket messages work as expected (i.e. either end can initate communication over the established WebSocket) so I know the remote server is working and it is not a firewall issue.


Is there anything special needed to make WebSockets work with a Launchd daemon?

This is almost certainly related to your main event loop. When you host the code in an app the UI framework (presumably AppKit) is running the run loop. When you host the code in a daemon, you are responsible for the main event loop. The best technique for this is

dispatchMain
(see the
dispatch_main
man page) but that does behave somewhat differently than AppKit’s run loop.

Network framework should be compatible with

dispatchMain
.

My first debugging step here would be to pull the relevant code out of your daemon and put it into a command-line tool. That isolate this issue from all the other weird stuff seen by

launchd
daemons, like running as root, and also lets you debug things in Xcode.

If you still see the problem in your command-line tool, you can then turn your attention to its main even loop.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Launchd daemon and WebSockets
 
 
Q