CFMessagePort objects provide a communications channel that can transmit arbitrary data between multiple threads or processes on the local machine.

You create a local message port with CFMessagePortCreateLocal(_:_:_:_:_:) and make it available to other processes by giving it a name, either when you create it or later with CFMessagePortSetName(_:_:). Other processes then connect to it using CFMessagePortCreateRemote(_:_:), specifying the name of the port.

To listen for messages, you need to create a run loop source with CFMessagePortCreateRunLoopSource(_:_:_:) and add it to a run loop with CFRunLoopAddSource(_:_:_:).

Your message port’s callback function will be called when a message arrives. To send data, you store the data in a CFData object and call CFMessagePortSendRequest(_:_:_:_:_:_:_:). You can optionally have the function wait for a reply and return the reply in another CFData object.

Message ports only support communication on the local machine. For network communication, you have to use a CFSocket object.


Configuring a CFMessagePort Object

func CFMessagePortCreateRunLoopSource(CFAllocator!, CFMessagePort!, CFIndex)

Creates a CFRunLoopSource object for a CFMessagePort object.

func CFMessagePortSetInvalidationCallBack(CFMessagePort!, CFMessagePortInvalidationCallBack!)

Sets the callback function invoked when a CFMessagePort object is invalidated.

func CFMessagePortSetName(CFMessagePort!, CFString!)

Sets the name of a local CFMessagePort object.

Using a Message Port

func CFMessagePortInvalidate(CFMessagePort!)

Invalidates a CFMessagePort object, stopping it from receiving or sending any more messages.

func CFMessagePortSetDispatchQueue(CFMessagePort!, DispatchQueue!)

Schedules callbacks for the specified message port on the specified dispatch queue.

Examining a Message Port

func CFMessagePortGetInvalidationCallBack(CFMessagePort!)

Returns the invalidation callback function for a CFMessagePort object.

func CFMessagePortGetName(CFMessagePort!)

Returns the name with which a CFMessagePort object is registered.

func CFMessagePortIsRemote(CFMessagePort!)

Returns a Boolean value that indicates whether a CFMessagePort object represents a remote port.

func CFMessagePortIsValid(CFMessagePort!)

Returns a Boolean value that indicates whether a CFMessagePort object is valid and able to send or receive messages.

Getting the CFMessagePort Type ID

func CFMessagePortGetTypeID()

Returns the type identifier for the CFMessagePort opaque type.



Callback invoked to process a message received on a CFMessagePort object.


Callback invoked when a CFMessagePort object is invalidated.

Data Types


A structure that contains program-defined data and callbacks with which you can configure a CFMessagePort object’s behavior.


A reference to a message port object.


CFMessagePortSendRequest Error Codes

Error codes for CFMessagePortSendRequest.