1# Publishing Common Events 2<!--Kit: Basic Services Kit--> 3<!--Subsystem: Notification--> 4<!--Owner: @michael_woo888--> 5<!--Designer: @dongqingran; @wulong158--> 6<!--Tester: @wanghong1997--> 7<!--Adviser: @huipeizi--> 8 9## When to Use 10 11You can use [publish()](../../reference/apis-basic-services-kit/js-apis-commonEventManager.md#commoneventmanagerpublish) to publish a common event, which can carry data for subscribers to parse and process. 12 13> **NOTE** 14> 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). 15 16 17## Available APIs 18 19For details about the APIs, see [commonEventManager.publish](../../reference/apis-basic-services-kit/js-apis-commonEventManager.md#commoneventmanagerpublish). 20 21| API | Description | 22| ------------------------------------------------------------ | ---------------------------- | 23| publish(event: string, callback: AsyncCallback<void\>) | Publishes a common event. | 24| publish(event: string, options: [CommonEventPublishData](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventPublishData.md), callback: AsyncCallback<void\>) | Publishes a common event with given attributes.| 25 26 27## Publishing a Common Event That Does Not Carry Information 28 29Common events that do not carry information can be published only as unordered common events. 30 311. Import the **commonEventManager** module. 32 33 ```ts 34 import { BusinessError, commonEventManager } from '@kit.BasicServicesKit'; 35 import { hilog } from '@kit.PerformanceAnalysisKit'; 36 37 const TAG: string = 'ProcessModel'; 38 const DOMAIN_NUMBER: number = 0xFF00; 39 ``` 40 412. Pass in the common event name and callback, and publish the event. 42 43 ```ts 44 // Publish the common event. Replace the event field with the actual event name. 45 commonEventManager.publish('event', (err: BusinessError) => { 46 if (err) { 47 hilog.error(DOMAIN_NUMBER, TAG, `Publish failed, code is ${JSON.stringify(err.code)}, message is ${JSON.stringify(err.message)}`); 48 } else { 49 //... 50 hilog.info(DOMAIN_NUMBER, TAG, `Publish success`); 51 } 52 }); 53 ``` 54 55 56## Publishing a Common Event That Carries Information 57 58Common 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). 59 601. Import the **commonEventManager** module. 61 62 ```ts 63 import { BusinessError, commonEventManager } from '@kit.BasicServicesKit'; 64 import { hilog } from '@kit.PerformanceAnalysisKit'; 65 66 const TAG: string = 'ProcessModel'; 67 const DOMAIN_NUMBER: number = 0xFF00; 68 ``` 69 702. Create the public event information to publish. 71 72 ```ts 73 // Attributes of a common event. 74 let options: commonEventManager.CommonEventPublishData = { 75 code: 1, // Result code of the common event. 76 data: 'initial data', // Initial data of the common event. 77 }; 78 ``` 79 803. Pass in the common event name, attributes of the common event, and callback, and publish the event. 81 82 ```ts 83 // Publish the common event. Replace the event field with the actual event name. 84 commonEventManager.publish('event', options, (err: BusinessError) => { 85 if (err) { 86 hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish common event. Code is ${err.code}, message is ${err.message}`); 87 } else { 88 //... 89 hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in publishing common event.`); 90 } 91 }); 92 ``` 93