<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "RealityKit",
  "identifier" : "/documentation/RealityKit/RealityViewContentProtocol/subscribe(to:on:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "RealityKit",
      "SwiftUI",
      "_RealityKit_SwiftUI"
    ],
    "preciseIdentifier" : "s:19_RealityKit_SwiftUI0A19ViewContentProtocolPAAE9subscribe2to2on_0A10Foundation17EventSubscriptionVqd__m_AG0L6Source_pSgyqd__ctAG0L0Rd__lF"
  },
  "title" : "subscribe(to:on:_:)"
}
-->

# subscribe(to:on:_:)

Subscribes to an event type, optionally limited to
events affecting a source entity or scene.

```
func subscribe<E>(to event: E.Type, on sourceObject: (any EventSource)?, _ handler: @escaping (E) -> Void) -> EventSubscription where E : Event
```

## Parameters

`event`

The event type to subscribe to.
For example, [`SceneEvents.Update`](/documentation/RealityKit/SceneEvents/Update) or [`ComponentEvents.DidActivate`](/documentation/RealityKit/ComponentEvents/DidActivate).

`sourceObject`

An optional source for the event, such as an entity or a scene.
Set to `nil` to listen for all events of the event type within the view content.

`handler`

A closure that runs when the `event` occurs.

## Return Value

An object that represents the subscription to this event stream.

---

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)