---
title: "subscribe(to:on:_:)"
framework: realitykit
role: symbol
role_heading: Instance Method
path: "realitykit/scene/subscribe(to:on:_:)"
---

# subscribe(to:on:_:)

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

## Declaration

```swift
@MainActor @preconcurrency func subscribe<E>(to event: E.Type, on sourceObject: (any EventSource)? = nil, _ handler: @escaping (E) -> Void) -> any Cancellable 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/DidAdd.
- `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 doc://com.apple.RealityKit/documentation/RealityKit/Scene.
- `handler`: A closure that runs when the event occurs.

## Return Value

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

## See Also

### Publishing and subscribing to events

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