1# Publishing Common Events 2 3 4## When to Use 5 6You can use [publish()](../reference/apis/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> 10> 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). 11 12 13## Available APIs 14 15For details about the APIs, see [API Reference](../reference/apis/js-apis-commonEventManager.md#commoneventmanagerpublish). 16 17| API| Description| 18| -------- | -------- | 19| publish(event: string, callback: AsyncCallback) | Publishes a common event.| 20| publish(event: string, options: [CommonEventPublishData](../reference/apis/js-apis-commonEventManager.md#commoneventpublishdata), callback: AsyncCallback) | Publishes a common event with given attributes.| 21 22 23## Publishing a Common Event That Does Not Carry Information 24 25Common events that do not carry information can be published only as unordered common events. 26 271. Import the **commonEventManager** module. 28 29 ```ts 30 import commonEventManager from '@ohos.commonEventManager'; 31 import Base from '@ohos.base'; 32 ``` 33 342. Pass in the common event name and callback, and publish the event. 35 36 ```ts 37 // Publish a common event. 38 commonEventManager.publish("usual.event.SCREEN_OFF", (err: Base.BusinessError) => { 39 if (err) { 40 console.error(`[CommonEvent] PublishCallBack err=${JSON.stringify(err)}`); 41 } else { 42 console.info(`[CommonEvent] Publish success`); 43 } 44 }); 45 ``` 46 47 48## Publishing a Common Event That Carries Information 49 50Common 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/js-apis-commonEventManager.md#commoneventpublishdata). 51 521. Import the **commonEventManager** module. 53 54 ```ts 55 import commonEventManager from '@ohos.commonEventManager'; 56 import Base from '@ohos.base'; 57 ``` 58 592. Pass in the common event name and callback, and publish the event. 60 61 ```ts 62 // Attributes of a common event. 63 let options: commonEventManager.CommonEventPublishData = { 64 code: 1, // Result code of the common event. 65 data: "initial data", // Result data of the common event. 66 } 67 ``` 68 693. Pass in the common event name, attributes of the common event, and callback, and publish the event. 70 71 ```ts 72 // Publish a common event. 73 commonEventManager.publish("usual.event.SCREEN_OFF", options, (err: Base.BusinessError) => { 74 if (err) { 75 console.error('[CommonEvent] PublishCallBack err=' + JSON.stringify(err)); 76 } else { 77 console.info('[CommonEvent] Publish success') 78 } 79 }); 80 ``` 81