<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreFoundation",
  "identifier" : "/documentation/CoreFoundation/CFMessagePort",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core Foundation"
    ],
    "preciseIdentifier" : "c:@T@CFMessagePortRef"
  },
  "title" : "CFMessagePort"
}
-->

# CFMessagePort

```
class CFMessagePort
```

## Overview

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(_:_:_:_:_:)`](/documentation/CoreFoundation/CFMessagePortCreateLocal(_:_:_:_:_:)) and make it available to other processes by giving it a name, either when you create it or later with [`CFMessagePortSetName(_:_:)`](/documentation/CoreFoundation/CFMessagePortSetName(_:_:)). Other processes then connect to it using [`CFMessagePortCreateRemote(_:_:)`](/documentation/CoreFoundation/CFMessagePortCreateRemote(_:_:)), specifying the name of the port.

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

> Important:
> If you want to tear down the connection, you must invalidate the port (using ``doc://com.apple.corefoundation/documentation/CoreFoundation/CFMessagePortInvalidate(_:)``) before releasing the runloop source and the message port object.

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(_:_:_:_:_:_:_:)`](/documentation/CoreFoundation/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`](/documentation/CoreFoundation/CFSocket) object.

## Topics

### Creating a CFMessagePort Object

[`CFMessagePortCreateLocal(_:_:_:_:_:)`](/documentation/CoreFoundation/CFMessagePortCreateLocal(_:_:_:_:_:))

Returns a local CFMessagePort object.

[`CFMessagePortCreateRemote(_:_:)`](/documentation/CoreFoundation/CFMessagePortCreateRemote(_:_:))

Returns a CFMessagePort object connected to a remote port.

### Configuring a CFMessagePort Object

[`CFMessagePortCreateRunLoopSource(_:_:_:)`](/documentation/CoreFoundation/CFMessagePortCreateRunLoopSource(_:_:_:))

Creates a CFRunLoopSource object for a CFMessagePort object.

[`CFMessagePortSetInvalidationCallBack(_:_:)`](/documentation/CoreFoundation/CFMessagePortSetInvalidationCallBack(_:_:))

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

[`CFMessagePortSetName(_:_:)`](/documentation/CoreFoundation/CFMessagePortSetName(_:_:))

Sets the name of a local CFMessagePort object.

### Using a Message Port

[`CFMessagePortInvalidate(_:)`](/documentation/CoreFoundation/CFMessagePortInvalidate(_:))

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

[`CFMessagePortSendRequest(_:_:_:_:_:_:_:)`](/documentation/CoreFoundation/CFMessagePortSendRequest(_:_:_:_:_:_:_:))

Sends a message to a remote CFMessagePort object.

[`CFMessagePortSetDispatchQueue(_:_:)`](/documentation/CoreFoundation/CFMessagePortSetDispatchQueue(_:_:))

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

### Examining a Message Port

[`CFMessagePortGetContext(_:_:)`](/documentation/CoreFoundation/CFMessagePortGetContext(_:_:))

Returns the context information for a CFMessagePort object.

[`CFMessagePortGetInvalidationCallBack(_:)`](/documentation/CoreFoundation/CFMessagePortGetInvalidationCallBack(_:))

Returns the invalidation callback function for a CFMessagePort object.

[`CFMessagePortGetName(_:)`](/documentation/CoreFoundation/CFMessagePortGetName(_:))

Returns the name with which a CFMessagePort object is registered.

[`CFMessagePortIsRemote(_:)`](/documentation/CoreFoundation/CFMessagePortIsRemote(_:))

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

[`CFMessagePortIsValid(_:)`](/documentation/CoreFoundation/CFMessagePortIsValid(_:))

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

### Getting the CFMessagePort Type ID

[`CFMessagePortGetTypeID()`](/documentation/CoreFoundation/CFMessagePortGetTypeID())

Returns the type identifier for the CFMessagePort opaque type.

### Callbacks

[`CFMessagePortCallBack`](/documentation/CoreFoundation/CFMessagePortCallBack)

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

[`CFMessagePortInvalidationCallBack`](/documentation/CoreFoundation/CFMessagePortInvalidationCallBack)

Callback invoked when a CFMessagePort object is invalidated.

### Data Types

[`CFMessagePortContext`](/documentation/CoreFoundation/CFMessagePortContext)

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

### Constants

[CFMessagePortSendRequest Error Codes](/documentation/CoreFoundation/1561514-cfmessageportsendrequest-error-c)

Error codes for `CFMessagePortSendRequest`.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)