# 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.

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

```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](/docs/platform/canvas-framework/guide/canvas-app-events.md)
- [Subscribing to Parent Events](/docs/platform/canvas-framework/guide/canvas-app-subscribe-code-example.md)
- [Unsubscribing from a Canvas App Event](/docs/platform/canvas-framework/guide/canvas-app-event-unsubscribe-code-example.md)
