---
schema_version: "1"
id: 80983880f3108178762c1ce65349d51fdf42a6290bd6c00e5614b996a81b4501
title: Subscribing to a Canvas App Event
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/canvas-framework/guides/platform-connect/using/events/canvas-app-event-subscribe-code-example.md
canonical_url: https://developer.salesforce.com/docs/platform/canvas-framework/guide/canvas-app-event-subscribe-code-example.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 238d265b483c276e5522406b2cdba037a6eb6eda6c0133745c5f3d12b284e412
products:
  - Canvas Developer Guide
  - Platform Developer Content
---

# Subscribing to a Canvas App Event

## Subscribing to a Custom Event

The following code example shows how to call the `subscribe` method to subscribe to a canvas app event.

<!-- otherprops: set_clipboard -->
```java
// Subscribe to a custom event.
Sfdc.canvas(function() {
    sr = JSON.parse('<%=signedRequestJson%>');
    Sfdc.canvas.client.subscribe(sr.client,
        {name : 'mynamespace.statusChanged', onData : function (event) {
            console.log("Subscribed to custom event ", event);
        }}
    );
});
```

## Subscribing to Multiple Custom Events

The following code example shows how to call the `subscribe` method to subscribe to multiple canvas app events. The events you subscribe to can be in different namespaces or might not have a namespace. When a canvas app subscribes to an event, it creates an association between an event (in the other canvas app) and a function (in the subscribing canvas app).

<!-- otherprops: set_clipboard -->
```java
// Subscribe to multiple events.
Sfdc.canvas(function() {
    sr = JSON.parse('<%=signedRequestJson%>');
    Sfdc.canvas.client.subscribe(sr.client, [
        {name : 'mynamespace.statusChanged', onData : handler1},
        {name : 'anothernamespace.tripCancelled', onData : handler2},
    ]);
});
```

Using the travel and expense and approval canvas app examples, your approvals canvas app has two functions: `handler1` and `handler2`. That canvas app then subscribes to two events in the travel and expense canvas app: `mynamespace.statusChanged` and `mynamespace.tripCancelled`. When the `mynamespace.statusChanged` event is received by the approvals app, function `handler1` is called. When the `anothernamespace.tripCancelled` event is received by the approvals app, function `handler2` is called.

**See Also**  


-   [Implementing Canvas App Events](https://developer.salesforce.com/docs/platform/canvas-framework/guide/canvas-app-events.md)
-   [Subscribing to Parent Events](https://developer.salesforce.com/docs/platform/canvas-framework/resizing/canvas-app-subscribe-code-example.md)
-   [Unsubscribing from a Canvas App Event](https://developer.salesforce.com/docs/platform/canvas-framework/guide/canvas-app-event-unsubscribe-code-example.md)

