• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using Emitter for Inter-Thread Communication
2
3[Emitter](../reference/apis/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.
4
5
6To develop the Emitter mode, perform the following steps:
7
8
91. Subscribe to an event.
10
11   ```ts
12   import emitter from "@ohos.events.emitter";
13
14   // Define an event with eventId 1.
15   let event = {
16       eventId: 1
17   };
18
19   // Trigger the callback after the event with eventId 1 is received.
20   let callback = (eventData) => {
21       console.info('event callback');
22   };
23
24   // Subscribe to the event with eventId 1.
25   emitter.on(event, callback);
26   ```
27
282. Emit the event.
29
30   ```ts
31   import emitter from "@ohos.events.emitter";
32
33   // Define an event with eventId 1 and priority Low.
34   let event = {
35       eventId: 1,
36       priority: emitter.EventPriority.LOW
37   };
38
39   let eventData = {
40       data: {
41           "content": "c",
42           "id": 1,
43           "isEmpty": false,
44       }
45   };
46
47   // Emit the event with eventId 1 and event content eventData.
48   emitter.emit(event, eventData);
49   ```
50