1e41f4b71Sopenharmony_ci# Publishing Common Events 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## When to Use 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciYou can use [publish()](../../reference/apis-basic-services-kit/js-apis-commonEventManager.md#commoneventmanagerpublish) to publish a custom common event, which can carry data for subscribers to parse and process. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci> **NOTE** 9e41f4b71Sopenharmony_ci> Subscribers can receive sticky common events that have been sent. However, they must subscribe to common events of other types before receiving them. For details about subscription, see [Subscribing to Common Events](common-event-subscription.md). 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## Available APIs 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciFor details about the APIs, see [API Reference](../../reference/apis-basic-services-kit/js-apis-commonEventManager.md#commoneventmanagerpublish). 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci| API | Description | 17e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ---------------------------- | 18e41f4b71Sopenharmony_ci| publish(event: string, callback: AsyncCallback) | Publishes a common event. | 19e41f4b71Sopenharmony_ci| publish(event: string, options: [CommonEventPublishData](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventPublishData.md), callback: AsyncCallback) | Publishes a common event with given attributes.| 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## Publishing a Common Event That Does Not Carry Information 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciCommon events that do not carry information can be published only as unordered common events. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci1. Import the **commonEventManager** module. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci ```ts 29e41f4b71Sopenharmony_ci import Base from '@ohos.base'; 30e41f4b71Sopenharmony_ci import commonEventManager from '@ohos.commonEventManager'; 31e41f4b71Sopenharmony_ci import promptAction from '@ohos.promptAction'; 32e41f4b71Sopenharmony_ci import hilog from '@ohos.hilog'; 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci const TAG: string = 'ProcessModel'; 35e41f4b71Sopenharmony_ci const DOMAIN_NUMBER: number = 0xFF00; 36e41f4b71Sopenharmony_ci ``` 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci2. Pass in the common event name and callback, and publish the event. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci ```ts 41e41f4b71Sopenharmony_ci // Publish the common event. Replace the event field with the actual event name. 42e41f4b71Sopenharmony_ci commonEventManager.publish('event', (err: Base.BusinessError) => { 43e41f4b71Sopenharmony_ci if (err) { 44e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `PublishCallBack err = ${JSON.stringify(err)}`); 45e41f4b71Sopenharmony_ci } else { 46e41f4b71Sopenharmony_ci //... 47e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `Publish success`); 48e41f4b71Sopenharmony_ci } 49e41f4b71Sopenharmony_ci }); 50e41f4b71Sopenharmony_ci ``` 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci## Publishing a Common Event That Carries Information 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ciCommon events that carry information can be published as unordered, ordered, and sticky common events, which are specified by the **isOrdered** and **isSticky** fields of [CommonEventPublishData](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventPublishData.md). 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci1. Import the **commonEventManager** module. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci ```ts 60e41f4b71Sopenharmony_ci import Base from '@ohos.base'; 61e41f4b71Sopenharmony_ci import commonEventManager from '@ohos.commonEventManager'; 62e41f4b71Sopenharmony_ci import hilog from '@ohos.hilog'; 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci const TAG: string = 'ProcessModel'; 65e41f4b71Sopenharmony_ci const DOMAIN_NUMBER: number = 0xFF00; 66e41f4b71Sopenharmony_ci ``` 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci2. Create the public event information to be released. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci ```ts 71e41f4b71Sopenharmony_ci // Attributes of a common event. 72e41f4b71Sopenharmony_ci let options: commonEventManager.CommonEventPublishData = { 73e41f4b71Sopenharmony_ci code: 1, // Result code of the common event. 74e41f4b71Sopenharmony_ci data: 'initial data', // Initial data of the common event. 75e41f4b71Sopenharmony_ci }; 76e41f4b71Sopenharmony_ci ``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci3. Pass in the common event name, attributes of the common event, and callback, and publish the event. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci ```ts 81e41f4b71Sopenharmony_ci // Publish the common event. Replace the event field with the actual event name. 82e41f4b71Sopenharmony_ci commonEventManager.publish('event', options, (err: Base.BusinessError) => { 83e41f4b71Sopenharmony_ci if (err) { 84e41f4b71Sopenharmony_ci hilog.error(DOMAIN_NUMBER, TAG, 'PublishCallBack err = ' + JSON.stringify(err)); 85e41f4b71Sopenharmony_ci } else { 86e41f4b71Sopenharmony_ci //... 87e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'Publish success'); 88e41f4b71Sopenharmony_ci } 89e41f4b71Sopenharmony_ci }); 90e41f4b71Sopenharmony_ci ``` 91