1e41f4b71Sopenharmony_ci# Using Emitter for Inter-Thread Communication 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci[Emitter](../../reference/apis-basic-services-kit/js-apis-emitter.md) provides APIs for sending and processing events between threads, including the APIs for processing events that are subscribed to in persistent or one-shot manner, unsubscribing from events, and emitting events to the event queue. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciTo develop the Emitter mode, perform the following steps: 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci1. Subscribe to an event. 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci ```ts 13e41f4b71Sopenharmony_ci import { emitter } from '@kit.BasicServicesKit'; 14e41f4b71Sopenharmony_ci import { promptAction } from '@kit.ArkUI'; 15e41f4b71Sopenharmony_ci import { hilog } from '@kit.PerformanceAnalysisKit'; 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci const TAG: string = 'ThreadModel'; 18e41f4b71Sopenharmony_ci const DOMAIN_NUMBER: number = 0xFF00; 19e41f4b71Sopenharmony_ci ``` 20e41f4b71Sopenharmony_ci ```ts 21e41f4b71Sopenharmony_ci // Define an event with eventId 1. 22e41f4b71Sopenharmony_ci let event: emitter.InnerEvent = { 23e41f4b71Sopenharmony_ci eventId: 1 24e41f4b71Sopenharmony_ci }; 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci // Trigger the callback after the event with eventId 1 is received. 27e41f4b71Sopenharmony_ci let callback = (eventData: emitter.EventData): void => { 28e41f4b71Sopenharmony_ci promptAction.showToast({ 29e41f4b71Sopenharmony_ci message: JSON.stringify(eventData) 30e41f4b71Sopenharmony_ci }); 31e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'event callback:' + JSON.stringify(eventData)); 32e41f4b71Sopenharmony_ci }; 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci // Subscribe to the event with eventId 1. 35e41f4b71Sopenharmony_ci emitter.on(event, callback); 36e41f4b71Sopenharmony_ci promptAction.showToast({ 37e41f4b71Sopenharmony_ci message: JSON.stringify('emitter subscribe success') 38e41f4b71Sopenharmony_ci }); 39e41f4b71Sopenharmony_ci ``` 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci2. Emit the event. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci ```ts 44e41f4b71Sopenharmony_ci import { emitter } from '@kit.BasicServicesKit'; 45e41f4b71Sopenharmony_ci ``` 46e41f4b71Sopenharmony_ci ```ts 47e41f4b71Sopenharmony_ci // Define an event with eventId 1 and priority Low. 48e41f4b71Sopenharmony_ci let event: emitter.InnerEvent = { 49e41f4b71Sopenharmony_ci eventId: 1, 50e41f4b71Sopenharmony_ci priority: emitter.EventPriority.LOW 51e41f4b71Sopenharmony_ci }; 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci let eventData: emitter.EventData = { 54e41f4b71Sopenharmony_ci data: { 55e41f4b71Sopenharmony_ci content: 'c', 56e41f4b71Sopenharmony_ci id: 1, 57e41f4b71Sopenharmony_ci isEmpty: false 58e41f4b71Sopenharmony_ci } 59e41f4b71Sopenharmony_ci }; 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci // Emit the event with eventId 1 and event content eventData. 62e41f4b71Sopenharmony_ci emitter.emit(event, eventData); 63e41f4b71Sopenharmony_ci ``` 64