---
schema_version: "1"
id: 2e6ce7efe719a9fe8f249465c0516d0acbd5dd08d1fe6e13ca455010aaa0d42b
title: Publish on a Message Channel
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/aura-platform/guides/aura/message-channel/message-channel-publish.md
canonical_url: https://developer.salesforce.com/docs/platform/aura-platform/guide/message-channel-publish.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 977046ad0c531d871e127a347e238962c03854610f3bf983e0197e6153a3fa04
products:
  - Lightning Aura Components Developer Guide
  - Platform Developer Content
---

# Publish on a Message Channel

To publish a message on a message channel, include a `lightning:messageChannel` component in your Aura component and use the `publish()` method in your Aura component's controller file.


In this example, the `lmsPublisherAuraComponent` from the [github.com/trailheadapps/lwc-recipes](https://github.com/trailheadapps/lwc-recipes) repo shows how to publish a message to notify subscribers on a Lightning page when a contact is selected.

To reference a message channel, add the `lightning:messageChannel` component to your Aura component. The component has a required `type` attribute, which is the name of the message channel.

```nolang
<!-- myComponent.cmp -->
<aura:component>
    <lightning:messageChannel type="SampleMessageChannel__c"/>
</aura:component>

```

To reference a message channel from an org that has a namespace, prefix the message channel name with the namespace: `<lightning:messageChannel type="Namespace__MessageChannelName__c"/>`.

This example shows how to publish a message on the `SampleMessageChannel__c` channel when a button is clicked.

In `myComponent.cmp`, we create two components, `lightning:button` and `lightning:messageChannel`. On `lightning:button`, the `onclick` handler calls the `handleClick()` JavaScript function in the controller. We assign the `aura:id` attribute to `lightning:messageChannel` to access the `publish()` method.

```nolang
<!-- myComponent.cmp -->
<aura:component>
    <lightning:button onclick="{! c.handleClick }"/>
    <lightning:messageChannel type="SampleMessageChannel__c"
        aura:id="sampleMessageChannel"/>
</aura:component>
```

```javascript
// myComponentController.js
({
    handleClick: function(cmp, event, helper) {
        var payload = {
            recordId: "some string",
            recordData: {
                value: "some value"
            }
        };
        cmp.find("sampleMessageChannel").publish(payload);
    }
})

```

In the controller, `handleClick()` contains the `payload` object. This object holds the message that gets sent on the `SampleMessageChannel__c` message channel. Here, the message is a `recordId` with the value "some string" and `recordData`, whose value is the key-value pair `value: "some value"`. Then, the controller finds the `lightning:messageChannel` component referenced in `myComponent.cmp` and calls `publish()` with the payload.

**note:** Lightning message service publishes messages to any subscribed component until the destroy phase of the component's lifecycle, even if the component isn't visible. Sometimes when you navigate away from a Lightning page, components are cached and not destroyed. These components still receive messages. For more information, see [lifecycle](https://developer.salesforce.com/docs/platform/aura-platform/components/components-lifecycle.md) and related [system events](https://developer.salesforce.com/docs/platform/aura-platform/events/events-system.md)

