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:&nbsp;string,&nbsp;callback:&nbsp;AsyncCallback<void\>) | Publishes a common event.              |
19| publish(event:&nbsp;string,&nbsp;options:&nbsp;[CommonEventPublishData](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventPublishData.md),&nbsp;callback:&nbsp;AsyncCallback<void\>) | 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 { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
30   import { hilog } from '@kit.PerformanceAnalysisKit';
31
32   const TAG: string = 'ProcessModel';
33   const DOMAIN_NUMBER: number = 0xFF00;
34   ```
35
362. Pass in the common event name and callback, and publish the event.
37
38   ```ts
39   // Publish the common event. Replace the event field with the actual event name.
40   commonEventManager.publish('event', (err: BusinessError) => {
41     if (err) {
42       hilog.error(DOMAIN_NUMBER, TAG, `Publish failed, code is ${JSON.stringify(err.code)}, message is ${JSON.stringify(err.message)}`);
43     } else {
44       //...
45       hilog.info(DOMAIN_NUMBER, TAG, `Publish success`);
46     }
47   });
48   ```
49
50
51## Publishing a Common Event That Carries Information
52
53Common 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).
54
551. Import the **commonEventManager** module.
56
57   ```ts
58   import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
59   import { hilog } from '@kit.PerformanceAnalysisKit';
60
61   const TAG: string = 'ProcessModel';
62   const DOMAIN_NUMBER: number = 0xFF00;
63   ```
64
652. Create the public event information to be released.
66
67   ```ts
68   // Attributes of a common event.
69   let options: commonEventManager.CommonEventPublishData = {
70     code: 1, // Result code of the common event.
71     data: 'initial data', // Initial data of the common event.
72   };
73   ```
74
753. Pass in the common event name, attributes of the common event, and callback, and publish the event.
76
77   ```ts
78   // Publish the common event. Replace the event field with the actual event name.
79   commonEventManager.publish('event', options, (err: BusinessError) => {
80     if (err) {
81       hilog.error(DOMAIN_NUMBER, TAG, 'PublishCallBack err = ' + JSON.stringify(err));
82     } else {
83       //...
84       hilog.info(DOMAIN_NUMBER, TAG, 'Publish success');
85     }
86   });
87   ```
88