<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "RealityKit",
  "identifier" : "/documentation/RealityKit/RealityViewContent/subscribe(to:on:componentType:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "RealityKit",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:19_RealityKit_SwiftUI0A11ViewContentV9subscribe2to2on13componentType_0A10Foundation17EventSubscriptionVxm_AH0M6Source_pSgAH9Component_pXpSgyxctAH0M0RzlF"
  },
  "title" : "subscribe(to:on:componentType:_:)"
}
-->

# subscribe(to:on:componentType:_:)

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

```
func subscribe<E>(to event: E.Type, on sourceObject: (any EventSource)?, componentType: (any Component.Type)?, _ 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 [`RealityViewContent`](/documentation/RealityKit/RealityViewContent).

`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 [`RealityViewContent`](/documentation/RealityKit/RealityViewContent).

`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)