<!--
{
  "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:componentType:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "RealityKit",
      "SwiftUI",
      "_RealityKit_SwiftUI"
    ],
    "preciseIdentifier" : "s:19_RealityKit_SwiftUI0A19ViewContentProtocolPAAE9subscribe2to13componentType_0A10Foundation17EventSubscriptionVqd__m_AG9Component_pXpSgyqd__ctAG0M0Rd__lF"
  },
  "title" : "subscribe(to:componentType:_:)"
}
-->

# subscribe(to:componentType:_:)

Subscribes to an event type, optionally limited to
a specific component type for component events.

```
func subscribe<E>(to event: E.Type, componentType: (any Component.Type)? = nil, _ 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).

`componentType`

An optional component type to filter events to if the event is of the type [`ComponentEvents`](/documentation/RealityKit/ComponentEvents).
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.

## Discussion

Events you can subscribe to including scene updates,
[`SceneEvents.Update`](/documentation/RealityKit/SceneEvents/Update), or when an animation
ends, [`AnimationEvents.PlaybackCompleted`](/documentation/RealityKit/AnimationEvents/PlaybackCompleted).

---

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)