Communication between ArkTS and CPP
ArkTS-to-CPP
Sending a Message on ArkTS
On ArkTS, you can obtain the rnInstance instance and call rnInstance.postMessageToCpp(name: string, payload: any) to send a message to CPP. This method has two parameters:
- name: message name, which is used to distinguish different messages during broadcast.
- payload: message content to be sent.
this.ctx.rnInstance.postMessageToCpp("SAMPLE_MESSAGE", payload);
To send a message from ArkTS to Component or TurboModule, use any of the following methods to send a message to CPP:
Receiving a Message on CPP
-
Method 1: Use
onMessageReceivedto receive a message.ComponentInstanceneeds to inheritArkTSMessageHub::Observerto receive a message.class xxxComponentInstance : public CppComponentInstance<facebook::react::xxxShadowNode>, public ArkTSMessageHub::Observer- Construct
ArkTSMessageHub::Observerin the constructor.xxxComponentInstance::xxxComponentInstance(Context context) : CppComponentInstance(std::move(context)), ArkTSMessageHub::Observer(m_deps->arkTSMessageHub) { ··· } - Implement the
onMessageReceivedmethod inComponentInstanceto receive a message. After a message is received, determine whether to handle the message based on the message name.void xxxViewComponentInstance::onMessageReceived( ArkTSMessage const& message) { if (message.name == "SAMPLE_MESSAGE") { ··· } }
-
Method 2: Use
handleArkTSMessageto receive a message.- Create and implement the
ArkTSMessageHandlerclass inPackage, and implement thehandleArkTSMessagemethod to handle themessage.class SampleArkTSMessageHandler : public ArkTSMessageHandler { void handleArkTSMessage(const Context& ctx) override { ··· }; }; - Override the
createArkTSMessageHandlersmethod of thePackageobject to return theArkTSMessageHandlerclass.std::vector<ArkTSMessageHandler::Shared> SamplePackage::createArkTSMessageHandlers() { return {std::make_shared<SampleArkTSMessageHandler>()}; }
- Create and implement the
CPP-to-ArkTS
Using postMessageToArkTS to Send a Message
-
Sending a message on CPP
CPP uses thepostMessageToArkTSmethod of thernInstanceobject to send an event to ArkTS. This method has two parameters:- name: string, which is the name of the message to be sent.
- payload: folly::dynamic, which is the data to be sent.
m_deps->rnInstance.lock()->postMessageToArkTS(messageName, messagePayload); -
Receiving a message on ArkTS
On ArkTS, you can subscribe to the message sent from CPP by usingcppEventEmitterinrnInstanceand handle the message.const unsubscribe = rnInstance.cppEventEmitter.subscribe("SAMPLE_MESSAGE", (value: object) => { ··· unsubscribe(); })
Using TurboModule to Send a Message (Not Recommended)
- Create
TurboModuleon ArkTS and implement the corresponding API. - CPP obtains the
TurboModuleobject. On CPP, you can obtain theTurboModuleobject by using thegetTurboModulemethod of thernInstanceobject and convert this object toArkTSTurboModule.auto TurboModule = m_deps->rnInstance.lock()->getTurboModule<xxxTurboModule>("moduleName"); auto arkTsTurboModule = std::dynamic_pointer_cast<rnoh::ArkTSTurboModule>(turboModule); - Handle parameters and call the corresponding method on ArkTS.
folly::dynamic payload = folly::dynamic::object; arkTsTurboMOudle->callSync(functionName, payload);