Simplest client server example code using IPC using XPC using swift on MacOS

I am from a C/C++, Python, Javascript background and recently started learning development on MacOS using Swift.

I want simple IPC(Inter Process Communication) between two processes, let's call them server process and client process.

I could not find any simple example showing use of XPC for IPC. Let me try to put my thinking and what I am trying to build.

Suppose I have server_process.swift

// This is XPC Server

import Foundation

func run_server_loop() {
    // I guess I have to use NSXPCListener e.g. XPC Service
}

func on_message(message) {
    // I have received message from client_server.swift
    // do whatever can be done with this message
    // send reply to client_server.swift

    if(message == "ping") {
        send("pong")
    } else {
        send("command not supported")
    }
}

// run loop to start listener
run_server_loop()

I would need client_server.swift

import Foundation

// This is XPC Client

func some_way_to_connect() {
    // some way to connect to server process
}

// connect to server which is running on server_process.swift
client = some_way_to_connect()

// send a message to server
client.send("ping")

I put my thoughts in pseudo code.

How can I achieve such communication using XPC using Swift?

I want simple IPC (Inter Process Communication) between two processes, let’s call them server process and client process.

XPC does not allow IPC between arbitrary processes. The server process in your example must be managed by launchd in a way that launchd knows about the XPC service is vends. That means that the server proces must be:

  • An XPC Service

  • A launchd daemon or agent

  • A Service Management login item

Are you planning to use one of those for your server process?

Share and Enjoy

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

My exact requirement is this …

Ah, I didn’t see that thread go by because I don’t monitor the Core Media tag (I know a lot about IPC but not a lot about Core Media in general).

IPC from a DAL plug-in is tricky because your DAL plug-in can be loaded by an arbitrary process and your code has to tolerate the execution environment of that process. So, imagine that you set up a launchd agent to publish an XPC service in the user’s GUI login context:

  • If your DAL plug-in is loaded in an non-sandboxed process, it’ll be able to access that service just fine.

  • If your DAL plug-in is loaded is a sandboxed process, access to that service will be blocked by the sandbox.

This isn’t just a problem with XPC. One of the goals of the sandbox is to restrict arbitrary IPC. A sandboxed app should only be able to perform unmediated IPC with code from the same team.

So, is your DAL plug-in targeted at specific apps? And, if so, are they sandboxed?

Share and Enjoy

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

one of DAL plugin which is a virtual cemera. I dont know if its sandboxed or not.

The sandbox applies to an entire process. Your plug-in doesn’t run in its own process. Rather, the plug-in is loaded by a host process. That host process may or may not be sandboxed, and if it is sandboxed then your IPC options are very limited.

You need to identify the apps that matter most to your DAL plug-in and check whether they are sandboxed or not.

Share and Enjoy

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

This DAL plugin are loaded as a virtual camera in …

I don’t know whether those specific third-party apps are sandboxed. However, it’s easy to work this out:

  1. Update your plug-in to log its process ID at startup. In fact, if you use the standard logging API then each log entry automatically includes the process ID.

  2. Load your plug-in in the app.

  3. Run Activity Monitor and look for that process ID.

  4. See whether the Sandbox column says Yes (if you can’t see that column, control click on the table header and enable it from the popup).

Share and Enjoy

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

But when the DAL Plugin tries to open a connection to that same XPC service, I get this error from the DAL Plugin "Couldn’t communicate with a helper application."

Which process is loading your DAL plug-in? And is that process sandboxed?

Share and Enjoy

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

Hello! So can you use XPC from an iPhone or iPad app to then communicate with that DAL plugin or how would you communicate with that DAL plugin from an iPhone? I know it is possible because that is what EpocCam does to stream video and audio for use in Skype, Zoom, etc. But I am having a difficult time determining how they do that. I thought maybe they might use Bonjour like a client/server but I do not see any bonjour processes for epoccam. But it has to be some kind of process communication from iPhone to Mac OS X and it happens over USB and WiFi.

Thank you! Adam

Can you please suggest any IOS client/Mac OS X server sample code that might show how to accomplish this? I am kind of stuck on how to get this working. Best regards, Adam

alokmahor wrote:

zoom meeting is loading that DAL plugin

And is that sandboxed? (You can see this in Activity Monitor.)


segfault01 wrote:

So can you use XPC from an iPhone or iPad app to then communicate with that DAL plugin

No. XPC is an inter-process communication API and does not work over the network.

how would you communicate with that DAL plugin from an iPhone?

Using a networking API.

Given that, I encourage you to start a new thread about your specific case. Tag it with Network so that I see it.

Share and Enjoy

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

Simplest client server example code using IPC using XPC using swift on MacOS
 
 
Q