How many connections can an XPC service have?

For example, let’s propose an XPC service that can connect to websites. Suppose that I want to connect to Apple.com, microsoft.com, and ibm.com. Can 3 service objects be made between the service and client? Or does the service have to return an ID for each web connection, with the client needing to specify which connection ID along with a command?

Answered by DTS Engineer in 865922022

There are a variety of ways you can approach this depending on the specific XPC API you’re using.

With any API you can take the approach you suggested, which is to have some sort of context identifier that you include in each message.

For most XPC APIs [1] you can create an anonymous endpoint and pass that back to the client. The client can create new connections to that endpoint’s listener. This allows you to set up completely independent connections.

This does have a cost in both memory and Mach ports. Mach ports are relatively plentiful, but I still recommend that you limit your usage. Using tens of endpoints will be absolutely fine, using hundreds should work but is pushing it, using thousands would be a mistake.

Finally, NSXPCConnection supports a proxy object mechanism. I’ve never actually tried this myself, but the basic idea is described in the Passing an Object By Proxy section on this page in the documentation archive.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] There’s one gotcha here, namely that with the low-level Swift API [2] the XPCEndpoint support was added later than the API itself, so if you use this technique you can’t deploy back to the oldest systems that supports that API.

[2] Using the terms from XPC Resources.

There are a variety of ways you can approach this depending on the specific XPC API you’re using.

With any API you can take the approach you suggested, which is to have some sort of context identifier that you include in each message.

For most XPC APIs [1] you can create an anonymous endpoint and pass that back to the client. The client can create new connections to that endpoint’s listener. This allows you to set up completely independent connections.

This does have a cost in both memory and Mach ports. Mach ports are relatively plentiful, but I still recommend that you limit your usage. Using tens of endpoints will be absolutely fine, using hundreds should work but is pushing it, using thousands would be a mistake.

Finally, NSXPCConnection supports a proxy object mechanism. I’ve never actually tried this myself, but the basic idea is described in the Passing an Object By Proxy section on this page in the documentation archive.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] There’s one gotcha here, namely that with the low-level Swift API [2] the XPCEndpoint support was added later than the API itself, so if you use this technique you can’t deploy back to the oldest systems that supports that API.

[2] Using the terms from XPC Resources.

How many connections can an XPC service have?
 
 
Q