---
title: "subscribe(to:componentType:_:)"
framework: realitykit
role: symbol
role_heading: Instance Method
path: "realitykit/realityviewcontentprotocol/subscribe(to:componenttype:_:)"
---

# subscribe(to:componentType:_:)

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

## Declaration

```swift
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, doc://com.apple.RealityKit/documentation/RealityKit/SceneEvents/Update or doc://com.apple.RealityKit/documentation/RealityKit/ComponentEvents/DidActivate.
- `componentType`: An optional component type to filter events to if the event is of the type doc://com.apple.RealityKit/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

Return Value An object that represents the subscription to this event stream.

## Discussion

Discussion Events you can subscribe to including scene updates, SceneEvents.Update, or when an animation ends, AnimationEvents.PlaybackCompleted.

## See Also

### Handling subscriptions

- [subscribe(to:on:_:)](realitykit/realityviewcontentprotocol/subscribe(to:on:_:).md)
- [subscribe(to:on:componentType:_:)](realitykit/realityviewcontentprotocol/subscribe(to:on:componenttype:_:).md)
