1# Publishing Common Events
2
3
4## When to Use
5
6You 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.
7
8> **NOTE**
9> 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).
10
11
12## Available APIs
13
14For details about the APIs, see [API Reference](../../reference/apis-basic-services-kit/js-apis-commonEventManager.md#commoneventmanagerpublish).
15
16| API                                                      | Description                    |
17| ------------------------------------------------------------ | ---------------------------- |
18| publish(event: string, callback: AsyncCallback) | Publishes a common event.              |
19| 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.|
20
21
22## Publishing a Common Event That Does Not Carry Information
23
24Common events that do not carry information can be published only as unordered common events.
25
261. Import the **commonEventManager** module.
27   
28   ```ts
29   import Base from '@ohos.base';
30   import commonEventManager from '@ohos.commonEventManager';
31   import promptAction from '@ohos.promptAction';
32   import hilog from '@ohos.hilog';
33
34   const TAG: string = 'ProcessModel';
35   const DOMAIN_NUMBER: number = 0xFF00;
36   ```
37
382. Pass in the common event name and callback, and publish the event.
39   
40   ```ts
41   // Publish the common event. Replace the event field with the actual event name.
42   commonEventManager.publish('event', (err: Base.BusinessError) => {
43     if (err) {
44       hilog.info(DOMAIN_NUMBER, TAG, `PublishCallBack err = ${JSON.stringify(err)}`);
45     } else {
46       //...
47       hilog.info(DOMAIN_NUMBER, TAG, `Publish success`);
48     }
49   });
50   ```
51
52
53## Publishing a Common Event That Carries Information
54
55Common 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).
56
571. Import the **commonEventManager** module.
58   
59   ```ts
60   import Base from '@ohos.base';
61   import commonEventManager from '@ohos.commonEventManager';
62   import hilog from '@ohos.hilog';
63
64   const TAG: string = 'ProcessModel';
65   const DOMAIN_NUMBER: number = 0xFF00;
66   ```
67
682. Create the public event information to be released.
69   
70   ```ts
71   // Attributes of a common event.
72   let options: commonEventManager.CommonEventPublishData = {
73     code: 1, // Result code of the common event.
74     data: 'initial data', // Initial data of the common event.
75   };
76   ```
77
783. Pass in the common event name, attributes of the common event, and callback, and publish the event.
79   
80   ```ts
81   // Publish the common event. Replace the event field with the actual event name.
82   commonEventManager.publish('event', options, (err: Base.BusinessError) => {
83     if (err) {
84       hilog.error(DOMAIN_NUMBER, TAG, 'PublishCallBack err = ' + JSON.stringify(err));
85     } else {
86       //...
87       hilog.info(DOMAIN_NUMBER, TAG, 'Publish success');
88     }
89   });
90   ```
91